google: more nits

Change-Id: I6b655c042e1758a49727909031847a86d092e303
This commit is contained in:
Patrick Jones
2020-12-17 15:44:00 -08:00
parent 80e2eea187
commit 9075f29749
2 changed files with 12 additions and 11 deletions

View File

@@ -68,13 +68,13 @@ type CredentialSource struct {
// instance determines the type of CredentialSource needed
func (c *Config) parse() baseCredentialSource {
if c.CredentialSource.File != "" {
return fileCredentialSource{File: c.CredentialSource.File}
return fileCredentialSource{File: c.CredentialSource.File, Format: c.CredentialSource.Format}
}
return nil
}
type baseCredentialSource interface {
retrieveSubjectToken(c *Config) (string, error)
subjectToken() (string, error)
}
// tokenSource is the source that handles external credentials.
@@ -91,7 +91,7 @@ func (ts tokenSource) Token() (*oauth2.Token, error) {
if credSource == nil {
return nil, fmt.Errorf("oauth2/google: unable to parse credential source")
}
subjectToken, err := credSource.retrieveSubjectToken(conf)
subjectToken, err := credSource.subjectToken()
if err != nil {
return nil, err
}

View File

@@ -14,33 +14,34 @@ import (
)
type fileCredentialSource struct {
File string
File string
Format format
}
func (cs fileCredentialSource) retrieveSubjectToken(c *Config) (string, error) {
func (cs fileCredentialSource) subjectToken() (string, error) {
tokenFile, err := os.Open(cs.File)
if err != nil {
return "", fmt.Errorf("oauth2/google: failed to open credential file %q\n", cs.File)
return "", fmt.Errorf("oauth2/google: failed to open credential file %q", cs.File)
}
defer tokenFile.Close()
tokenBytes, err := ioutil.ReadAll(tokenFile)
if err != nil {
return "", fmt.Errorf("oauth2/google: failed to read credential file; %q", err)
return "", fmt.Errorf("oauth2/google: failed to read credential file: %v", err)
}
tokenBytes = bytes.TrimSpace(tokenBytes)
var output string
switch c.CredentialSource.Format.Type {
switch cs.Format.Type {
case "json":
jsonData := make(map[string]interface{})
err = json.Unmarshal(tokenBytes, &jsonData)
if err != nil {
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file; %q", err)
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err)
}
if val, ok := jsonData[c.CredentialSource.Format.SubjectTokenFieldName]; !ok {
if val, ok := jsonData[cs.Format.SubjectTokenFieldName]; !ok {
return "", errors.New("oauth2/google: provided subject_token_field_name not found in credentials")
} else {
token, ok := val.(string)
if ok {
if !ok {
return "", errors.New("oauth2/google: improperly formatted subject token")
}
output = token