Example #1
0
func (s *credentialsSuite) TestNewCredentials(c *gc.C) {
	values := map[string]string{
		google.OSEnvClientID:    "abc",
		google.OSEnvClientEmail: "*****@*****.**",
		google.OSEnvPrivateKey:  "<some-key>",
	}
	creds, err := google.NewCredentials(values)
	c.Assert(err, jc.ErrorIsNil)

	jsonKey := creds.JSONKey
	creds.JSONKey = nil
	c.Check(creds, jc.DeepEquals, &google.Credentials{
		ClientID:    "abc",
		ClientEmail: "*****@*****.**",
		PrivateKey:  []byte("<some-key>"),
	})
	data := make(map[string]string)
	err = json.Unmarshal(jsonKey, &data)
	c.Assert(err, jc.ErrorIsNil)
	c.Check(data, jc.DeepEquals, map[string]string{
		"type":         "service_account",
		"client_id":    "abc",
		"client_email": "*****@*****.**",
		"private_key":  "<some-key>",
	})
}
Example #2
0
func (s *credentialsSuite) TestNewCredentialsUnrecognized(c *gc.C) {
	values := map[string]string{
		"spam": "eggs",
	}
	_, err := google.NewCredentials(values)

	c.Check(err, jc.Satisfies, errors.IsNotSupported)
}
Example #3
0
func createCredsFile(c *gc.C, path string) string {
	if path == "" {
		dir := c.MkDir()
		path = filepath.Join(dir, "creds.json")
	}
	creds, err := google.NewCredentials(sampleCredentialAttributes)
	c.Assert(err, jc.ErrorIsNil)
	err = ioutil.WriteFile(path, creds.JSONKey, 0644)
	c.Assert(err, jc.ErrorIsNil)
	return path
}
Example #4
0
func (s *credentialsSuite) TestCredentialsValues(c *gc.C) {
	original := map[string]string{
		google.OSEnvClientID:    "abc",
		google.OSEnvClientEmail: "*****@*****.**",
		google.OSEnvPrivateKey:  "<some-key>",
	}
	creds, err := google.NewCredentials(original)
	c.Assert(err, jc.ErrorIsNil)
	values := creds.Values()

	c.Check(values, jc.DeepEquals, original)
}
Example #5
0
func (s *credentialsSuite) TestNewCredentialsValidates(c *gc.C) {
	values := map[string]string{
		google.OSEnvClientEmail: "*****@*****.**",
		google.OSEnvPrivateKey:  "<some-key>",
		google.OSEnvProjectID:   "yup",
	}
	_, err := google.NewCredentials(values)
	// This error comes from Credentials.Validate so by implication
	// if we're getting this error, validation is being performed.
	c.Check(err, gc.ErrorMatches, `invalid config value \(\) for "GCE_CLIENT_ID": missing ClientID`)
	c.Assert(err, jc.Satisfies, google.IsInvalidConfigValueError)
}
Example #6
0
// parseCredentials extracts the OAuth2 info from the config from the
// individual fields (falling back on the JSON file).
func parseCredentials(cfg *config.Config) (*google.Credentials, error) {
	attrs := cfg.UnknownAttrs()
	values := make(map[string]string)
	for _, field := range configAuthFields {
		if existing, ok := attrs[field].(string); ok && existing != "" {
			for key, candidate := range osEnvFields {
				if field == candidate {
					values[key] = existing
					break
				}
			}
		}
	}
	return google.NewCredentials(values)
}
Example #7
0
// parseCredentials extracts the OAuth2 info from the config from the
// individual fields (falling back on the JSON file).
func parseCredentials(cfg *config.Config) (*google.Credentials, error) {
	attrs := cfg.UnknownAttrs()

	// Try the auth fields first.
	values := make(map[string]string)
	for _, field := range configAuthFields {
		if existing, ok := attrs[field].(string); ok && existing != "" {
			for key, candidate := range osEnvFields {
				if field == candidate {
					values[key] = existing
					break
				}
			}
		}
	}
	if len(values) > 0 {
		creds, err := google.NewCredentials(values)
		if err != nil {
			return nil, errors.Trace(err)
		}
		return creds, nil
	}

	// Fall back to the auth file.
	filename, ok := attrs[cfgAuthFile].(string)
	if !ok || filename == "" {
		// The missing credentials will be caught later.
		return nil, nil
	}
	authFile, err := os.Open(filename)
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer authFile.Close()
	creds, err := google.ParseJSONKey(authFile)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return creds, nil
}
Example #8
0
func (s *credentialsSuite) TestNewCredentialsEmpty(c *gc.C) {
	creds, err := google.NewCredentials(nil)
	c.Assert(err, jc.ErrorIsNil)

	c.Check(creds, jc.DeepEquals, &google.Credentials{})
}