Skip to content

Commit d73b98e

Browse files
committed
Improve credentials
1 parent 32502e2 commit d73b98e

File tree

2 files changed

+45
-62
lines changed

2 files changed

+45
-62
lines changed

internal/config/config.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,38 @@ import (
2626

2727
// searchConfigDir looks for a configuration file in different directories in the following order:
2828
// current working directory, parents of the current working directory, arduino15 default directory.
29-
// Returns a nil string if no config file has been found, without raising errors.
29+
// Returns empty string and false if no config file has been found, without raising errors.
3030
// Returns an error if any problem is encountered during the file research which prevents
3131
// to understand whether a config file exists or not.
32-
func searchConfigDir(confname string) (*string, error) {
32+
func searchConfigDir(confname string) (dir string, found bool, err error) {
3333
// Search in current directory and its parents.
3434
cwd, err := paths.Getwd()
3535
if err != nil {
36-
return nil, err
36+
return "", false, err
3737
}
3838
// Don't let bad naming mislead you, cwd.Parents()[0] is cwd itself so
3939
// we look in the current directory first and then on its parents.
4040
for _, path := range cwd.Parents() {
4141
logrus.Infof("Looking for %s in %s", confname, path)
4242
if file, found := configFileInDir(confname, path); found {
4343
logrus.Infof("Found %s at %s", confname, file)
44-
p := path.String()
45-
return &p, nil
44+
return path.String(), true, nil
4645
}
4746
}
4847

4948
// Search in arduino's default data directory.
5049
arduino15, err := arduino.DataDir()
5150
if err != nil {
52-
return nil, err
51+
return "", false, err
5352
}
5453
logrus.Infof("Looking for %s in %s", confname, arduino15)
5554
if file, found := configFileInDir(confname, arduino15); found {
5655
logrus.Infof("%s found at %s", confname, file)
57-
p := arduino15.String()
58-
return &p, nil
56+
return arduino15.String(), true, nil
5957
}
6058

6159
// Didn't find config file in the current directory, its parents or in arduino15"
62-
return nil, nil
60+
return "", false, nil
6361
}
6462

6563
// configFileInDir looks for a configuration file in the passed directory.

internal/config/credentials.go

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ const (
3838
CredentialsFilename = "arduino-cloud-credentials"
3939
)
4040

41-
// SetDefaultCredentials sets the default credentials values.
42-
func SetDefaultCredentials(settings *viper.Viper) {
41+
// SetEmptyCredentials sets the default credentials values to empty strings.
42+
func SetEmptyCredentials(settings *viper.Viper) {
4343
// Client ID
4444
settings.SetDefault("client", "")
4545
// Secret
@@ -79,57 +79,62 @@ func (c *Credentials) IsEmpty() bool {
7979

8080
// RetrieveCredentials looks for credentials in
8181
// environment variables or in credentials file.
82-
// Returns error if no credentials are found.
82+
// Returns error if no credentials are found or
83+
// if found credentials are invalid.
8384
func RetrieveCredentials() (*Credentials, error) {
8485
// Credentials extracted from environment has highest priority
8586
logrus.Info("Looking for credentials in environment variables")
8687
c, err := fromEnv()
8788
if err != nil {
8889
return nil, fmt.Errorf("reading credentials from environment variables: %w", err)
8990
}
90-
// Return credentials only if found
91-
if c != nil {
92-
logrus.Info("Credentials found in environment variables")
91+
// Return credentials if found in env
92+
if !c.IsEmpty() {
93+
// Return error if credentials are found but are not valid
94+
if err := c.Validate(); err != nil {
95+
return nil, fmt.Errorf(
96+
"credentials retrieved from environment variables with prefix '%s' are not valid: %w", EnvPrefix, err,
97+
)
98+
}
99+
logrus.Infof("Credentials found in environment variables with prefix '%s'", EnvPrefix)
93100
return c, nil
94101
}
95102

96103
logrus.Info("Looking for credentials in file system")
97-
c, err = fromFile()
104+
filepath, found, err := searchConfigDir(CredentialsFilename)
98105
if err != nil {
99-
return nil, fmt.Errorf("reading credentials from file: %w", err)
106+
return nil, fmt.Errorf("can't get credentials directory: %w", err)
100107
}
101-
if c != nil {
102-
return c, nil
108+
if !found {
109+
return nil, fmt.Errorf(
110+
"credentials have not been found neither in environment variables " +
111+
"nor in the current directory, its parents or in arduino15",
112+
)
103113
}
104114

105-
return nil, fmt.Errorf(
106-
"credentials have not been found neither in environment variables " +
107-
"nor in the current directory, its parents or in arduino15",
108-
)
109-
}
110-
111-
// fromFile looks for a credentials file.
112-
// If a credentials file is not found, it returns nil credentials without raising errors.
113-
// If invalid credentials file is found, it returns an error.
114-
func fromFile() (*Credentials, error) {
115-
// Looks for a credentials file
116-
configDir, err := searchConfigDir(CredentialsFilename)
115+
c, err = fromFile(filepath)
117116
if err != nil {
118-
return nil, fmt.Errorf("can't get credentials directory: %w", err)
117+
return nil, fmt.Errorf("reading credentials from file %s: %w", filepath, err)
119118
}
120-
// Return nil credentials if no config file is found
121-
if configDir == nil {
122-
return nil, nil
119+
// Return error if credentials are not valid
120+
if err := c.Validate(); err != nil {
121+
return nil, fmt.Errorf(
122+
"credentials retrieved from file %s are not valid: %w", filepath, err,
123+
)
123124
}
125+
return c, nil
126+
}
124127

128+
// fromFile looks for a credentials file.
129+
func fromFile(filepath string) (*Credentials, error) {
125130
v := viper.New()
126131
v.SetConfigName(CredentialsFilename)
127-
v.AddConfigPath(*configDir)
128-
err = v.ReadInConfig()
132+
v.AddConfigPath(filepath)
133+
err := v.ReadInConfig()
129134
if err != nil {
130135
err = fmt.Errorf(
131136
"credentials file found at %s but cannot read its content: %w",
132-
*configDir,
137+
filepath,
133138
err,
134139
)
135140
return nil, err
@@ -140,26 +145,18 @@ func fromFile() (*Credentials, error) {
140145
if err != nil {
141146
return nil, fmt.Errorf(
142147
"credentials file found at %s but cannot unmarshal it: %w",
143-
*configDir,
144-
err,
145-
)
146-
}
147-
if err = cred.Validate(); err != nil {
148-
return nil, fmt.Errorf(
149-
"credentials file found at %s but is not valid: %w",
150-
*configDir,
148+
filepath,
151149
err,
152150
)
153151
}
154152
return cred, nil
155153
}
156154

157-
// fromEnv looks for credentials in environment variables.
158-
// If credentials are not found, it returns nil credentials without raising errors.
159-
// If invalid credentials are found, it returns an error.
155+
// fromEnv retrieves credentials from environment variables.
156+
// Returns empty credentials if not found.
160157
func fromEnv() (*Credentials, error) {
161158
v := viper.New()
162-
SetDefaultCredentials(v)
159+
SetEmptyCredentials(v)
163160
v.SetEnvPrefix(EnvPrefix)
164161
v.AutomaticEnv()
165162

@@ -168,17 +165,5 @@ func fromEnv() (*Credentials, error) {
168165
if err != nil {
169166
return nil, fmt.Errorf("cannot unmarshal credentials from environment variables: %w", err)
170167
}
171-
172-
if cred.IsEmpty() {
173-
return nil, nil
174-
}
175-
176-
if err = cred.Validate(); err != nil {
177-
return nil, fmt.Errorf(
178-
"credentials retrieved from environment variables with prefix '%s' are not valid: %w",
179-
EnvPrefix,
180-
err,
181-
)
182-
}
183168
return cred, nil
184169
}

0 commit comments

Comments
 (0)