corso/src/cli/config/account.go
Hitesh Pattanayak 52c788eac5
check path equivalency (#4635)
<!-- PR description-->

This change is to address the path related issue that comes up while `corso repo connect filesystem`.
Below are the steps to reciprocate:
1. Initialize repo with filesystem provider (Notice that the path has a trailing slash): 
command: `./corso repo init filesystem --path ~/Documents/Backups/`
output:
Logging to file: ~/.cache/corso/logs/2023-11-09T05-21-57Z.log
Connecting to M365:         done 
Initializing repository   
Initialized a repository at path ~/Documents/Backups/

2. Connect repo with filesystem provider:
command: `./corso repo connect filesystem --path ~/Documents/Backups/`
output:
Logging to file: ~/.cache/corso/logs/2023-11-09T05-23-30Z.log
Error: retrieving storage provider details: verifying storage configs in corso config file: verifying configs in corso config file: value of path (~/Documents/Backups) does not match corso configuration value (~/Documents/Backups/)

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [x] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [x]  Unit test
- [x] 💚 E2E
2023-11-10 19:59:12 +00:00

118 lines
3.3 KiB
Go

package config
import (
"os"
"github.com/alcionai/clues"
"github.com/spf13/viper"
"github.com/alcionai/corso/src/cli/flags"
"github.com/alcionai/corso/src/internal/common/str"
"github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/credentials"
)
// prerequisite: readRepoConfig must have been run prior to this to populate the global viper values.
func m365ConfigsFromViper(vpr *viper.Viper) (account.M365Config, error) {
var m365 account.M365Config
m365.AzureClientID = vpr.GetString(account.AzureClientID)
m365.AzureClientSecret = vpr.GetString(account.AzureSecret)
m365.AzureTenantID = vpr.GetString(account.AzureTenantIDKey)
return m365, nil
}
func m365Overrides(in map[string]string) map[string]string {
return map[string]string{
account.AzureTenantID: in[account.AzureTenantID],
account.AccountProviderTypeKey: in[account.AccountProviderTypeKey],
}
}
// add m365 config key names that require path related validations
var m365PathKeys = []string{}
// configureAccount builds a complete account configuration from a mix of
// viper properties and manual overrides.
func configureAccount(
vpr *viper.Viper,
readConfigFromViper bool,
matchFromConfig bool,
overrides map[string]string,
) (account.Account, error) {
var (
m365Cfg account.M365Config
m365 credentials.M365
acct account.Account
err error
)
if readConfigFromViper {
m365Cfg, err = m365ConfigsFromViper(vpr)
if err != nil {
return acct, clues.Wrap(err, "reading m365 configs from corso config file")
}
if matchFromConfig {
providerType := vpr.GetString(account.AccountProviderTypeKey)
if providerType != account.ProviderM365.String() {
return acct, clues.New("unsupported account provider: [" + providerType + "]")
}
if err := mustMatchConfig(vpr, m365Overrides(overrides), m365PathKeys); err != nil {
return acct, clues.Wrap(err, "verifying m365 configs in corso config file")
}
}
}
// compose the m365 config and credentials
m365 = GetM365(m365Cfg)
if err := m365.Validate(); err != nil {
return acct, clues.Wrap(err, "validating m365 credentials")
}
m365Cfg = account.M365Config{
M365: m365,
AzureTenantID: str.First(
overrides[account.AzureTenantID],
flags.AzureClientTenantFV,
os.Getenv(account.AzureTenantID),
m365Cfg.AzureTenantID),
}
// ensure required properties are present
if err := requireProps(map[string]string{
credentials.AzureClientID: m365Cfg.M365.AzureClientID,
credentials.AzureClientSecret: m365Cfg.M365.AzureClientSecret,
account.AzureTenantID: m365Cfg.AzureTenantID,
}); err != nil {
return acct, err
}
// build the account
acct, err = account.NewAccount(account.ProviderM365, m365Cfg)
if err != nil {
return acct, clues.Wrap(err, "retrieving m365 account configuration")
}
return acct, nil
}
// M365 is a helper for aggregating m365 secrets and credentials.
func GetM365(m365Cfg account.M365Config) credentials.M365 {
AzureClientID := str.First(
flags.AzureClientIDFV,
os.Getenv(credentials.AzureClientID),
m365Cfg.AzureClientID)
AzureClientSecret := str.First(
flags.AzureClientSecretFV,
os.Getenv(credentials.AzureClientSecret),
m365Cfg.AzureClientSecret)
return credentials.M365{
AzureClientID: AzureClientID,
AzureClientSecret: AzureClientSecret,
}
}