From 0ee300c3e03f4df88eb7c98fcb30d3cb4c4cf689 Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Fri, 3 Jan 2020 16:08:25 -0800 Subject: [PATCH] Cloudprober OAuth: Add support for JWT as access token. (#341) * Cloudprober OAuth: Add support for JWT as access token. PiperOrigin-RevId: 287337684 --- common/oauth/oauth.go | 8 +++ common/oauth/oauth_test.go | 102 ++++++++++++++++++++++++++++++++ common/oauth/proto/config.pb.go | 70 ++++++++++++++-------- common/oauth/proto/config.proto | 7 +++ 4 files changed, 163 insertions(+), 24 deletions(-) create mode 100644 common/oauth/oauth_test.go diff --git a/common/oauth/oauth.go b/common/oauth/oauth.go index 21939aea..5d9e9747 100644 --- a/common/oauth/oauth.go +++ b/common/oauth/oauth.go @@ -52,6 +52,14 @@ func TokenSourceFromConfig(c *configpb.Config, l *logger.Logger) (oauth2.TokenSo return nil, fmt.Errorf("error reading Google Credentials file (%s): %v", f, err) } + aud := c.GetGoogleCredentials().GetAudience() + if aud != "" || c.GetGoogleCredentials().GetJwtAsAccessToken() { + if !c.GetGoogleCredentials().GetJwtAsAccessToken() { + return nil, fmt.Errorf("oauth: audience (%s) should only be set if jwt_as_access_token is set to true", aud) + } + return google.JWTAccessTokenSourceFromJSON(jsonKey, aud) + } + creds, err := google.CredentialsFromJSON(context.Background(), jsonKey, c.GetGoogleCredentials().GetScope()...) if err != nil { return nil, fmt.Errorf("error parsing Google Credentials file (%s): %v", f, err) diff --git a/common/oauth/oauth_test.go b/common/oauth/oauth_test.go new file mode 100644 index 00000000..34d30f23 --- /dev/null +++ b/common/oauth/oauth_test.go @@ -0,0 +1,102 @@ +// Copyright 2019 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oauth + +import ( + "fmt" + "io/ioutil" + "strings" + "testing" + + "github.com/golang/protobuf/proto" + configpb "github.com/google/cloudprober/common/oauth/proto" +) + +func createTempFile(t *testing.T, b []byte) string { + tmpfile, err := ioutil.TempFile("", "") + if err != nil { + t.Fatal(err) + return "" + } + + defer tmpfile.Close() + if _, err := tmpfile.Write(b); err != nil { + t.Fatal(err) + } + + return tmpfile.Name() +} + +var testPrivateKey = `-----BEGIN RSA PRIVATE KEY----- +MIIBPAIBAAJBAN6ErRPkzBWt+R+kMtbbAgmFal+ZbVoWSJDzsLFwdzfy0QaI6Svq +g4zpfn/H7lXi5MPJ+OWWhFy2DRD0L01PF8kCAwEAAQJBAM59MF+Vog08NEI4jTT0 +Zx+OvveX2PIQW6anfQAr7XXsEo910bPjb9YdfFaHyQCS8aIYeQ7vXD8tV6Vlu93B +LkECIQD77dd8JWEZp2ZCt0SpN6mPcNOvVoXhvdKp9SiqMorn0wIhAOIdK5hbx/d+ +rextXrNWAeT2PrWxYN7FX1neuAzebrxzAiEA9b9vuQlZa8XwqdnOX2cNvv+nbt1u +4eLiMaoVDdkZyMMCIQDRXslbTsEevsI1RiCGVoFyjUEL5K8aGBBumvg5kk1fWQIg +X5mM0KsPPfa9wLSGj6CPt2c3skhQu/k2FjMASmaQbZw= +-----END RSA PRIVATE KEY-----` + +func testJSONKey() string { + keyTmpl := `{ + "type": "service_account", + "project_id": "cloud-nat-prober", + "private_key_id": "testprivateid", + "private_key": "%s", + "client_email": "test-consumer@test-project.iam.gserviceaccount.com", + "client_id": "testclientid", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-consumer%%40test-project.iam.gserviceaccount.com" +} +` + return fmt.Sprintf(keyTmpl, strings.Replace(testPrivateKey, "\n", "\\n", -1)) +} + +func TestGoogleCredentials(t *testing.T) { + jsonF := createTempFile(t, []byte(testJSONKey())) + + googleC := &configpb.GoogleCredentials{ + JsonFile: proto.String(jsonF), + } + + c := &configpb.Config{ + Type: &configpb.Config_GoogleCredentials{ + GoogleCredentials: googleC, + }, + } + + _, err := TokenSourceFromConfig(c, nil) + if err != nil { + t.Errorf("Config: %v, Unexpected error: %v", c, err) + } + + // Set audience, it should fail as jwt_as_access_token is not set. + googleC.Audience = proto.String("test-audience") + + _, err = TokenSourceFromConfig(c, nil) + if err == nil { + t.Errorf("Config: %v, Expected error, but got none.", c) + } + + // Set jwt_as_access_token, no errors now. + googleC.JwtAsAccessToken = proto.Bool(true) + + _, err = TokenSourceFromConfig(c, nil) + if err != nil { + t.Errorf("Config: %v, Unexpected error: %v", c, err) + } +} diff --git a/common/oauth/proto/config.pb.go b/common/oauth/proto/config.pb.go index 1b93bb54..f8f4a9a3 100644 --- a/common/oauth/proto/config.pb.go +++ b/common/oauth/proto/config.pb.go @@ -212,8 +212,13 @@ func (*BearerToken) XXX_OneofWrappers() []interface{} { // Google credentials in JSON format. We simply use oauth2/google package to // use these credentials. type GoogleCredentials struct { - JsonFile *string `protobuf:"bytes,1,opt,name=json_file,json=jsonFile" json:"json_file,omitempty"` - Scope []string `protobuf:"bytes,2,rep,name=scope" json:"scope,omitempty"` + JsonFile *string `protobuf:"bytes,1,opt,name=json_file,json=jsonFile" json:"json_file,omitempty"` + Scope []string `protobuf:"bytes,2,rep,name=scope" json:"scope,omitempty"` + // Use encoded JWT directly as access token, instead of implementing the whole + // OAuth2.0 flow. + JwtAsAccessToken *bool `protobuf:"varint,4,opt,name=jwt_as_access_token,json=jwtAsAccessToken" json:"jwt_as_access_token,omitempty"` + // Audience works only if jwt_as_access_token is true. + Audience *string `protobuf:"bytes,3,opt,name=audience" json:"audience,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -258,6 +263,20 @@ func (m *GoogleCredentials) GetScope() []string { return nil } +func (m *GoogleCredentials) GetJwtAsAccessToken() bool { + if m != nil && m.JwtAsAccessToken != nil { + return *m.JwtAsAccessToken + } + return false +} + +func (m *GoogleCredentials) GetAudience() string { + if m != nil && m.Audience != nil { + return *m.Audience + } + return "" +} + func init() { proto.RegisterType((*Config)(nil), "cloudprober.oauth.Config") proto.RegisterType((*BearerToken)(nil), "cloudprober.oauth.BearerToken") @@ -269,26 +288,29 @@ func init() { } var fileDescriptor_0abe588b78ed21bf = []byte{ - // 329 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x8f, 0x41, 0x6b, 0xea, 0x40, - 0x10, 0xc7, 0x4d, 0xf4, 0x89, 0x8e, 0xef, 0x92, 0x7d, 0x1e, 0x02, 0x0f, 0x1e, 0x22, 0xef, 0xe0, - 0x29, 0x91, 0x52, 0x7a, 0x28, 0xf4, 0x50, 0x05, 0x6b, 0xaf, 0xb1, 0xbd, 0xf4, 0x12, 0x92, 0x71, - 0x8c, 0x69, 0x93, 0x9d, 0xb0, 0xbb, 0x11, 0xfa, 0x91, 0x0a, 0xfd, 0x90, 0x25, 0x1b, 0x41, 0xa9, - 0x3d, 0xfe, 0xe6, 0xff, 0xdb, 0x99, 0xff, 0xc2, 0x5d, 0x96, 0x9b, 0x7d, 0x9d, 0x06, 0xc8, 0x65, - 0x98, 0x31, 0x67, 0x05, 0x85, 0x58, 0x70, 0xbd, 0xad, 0x14, 0xa7, 0xa4, 0x42, 0xe4, 0xb2, 0x64, - 0x19, 0x72, 0x52, 0x9b, 0x7d, 0x58, 0x29, 0x36, 0x1c, 0x22, 0xcb, 0x5d, 0x9e, 0x05, 0x16, 0x84, - 0x77, 0x26, 0x07, 0xd6, 0x9a, 0x7e, 0x3a, 0xd0, 0x5f, 0x5a, 0x47, 0x2c, 0xe1, 0x77, 0x4a, 0x89, -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ce, 0x86, 0x51, 0x0b, 0x5f, 0x01, 0x00, 0x00, 0xff, - 0xff, 0x11, 0x5b, 0x14, 0xda, 0xf5, 0x01, 0x00, 0x00, + // 376 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0x31, 0x8f, 0xd3, 0x40, + 0x10, 0x85, 0xe3, 0x24, 0x44, 0xc9, 0x84, 0x82, 0xec, 0xa5, 0xb0, 0x40, 0x42, 0x51, 0x44, 0x91, + 0x06, 0xfb, 0x84, 0x10, 0x05, 0x12, 0x45, 0x12, 0x09, 0x42, 0xbb, 0x07, 0x0d, 0xcd, 0xca, 0x1e, + 0x4f, 0x1c, 0x07, 0x7b, 0xc7, 0xda, 0x5d, 0xdf, 0x89, 0x9f, 0xc1, 0xcf, 0x40, 0xe2, 0x47, 0x22, + 0xaf, 0x2d, 0x72, 0x22, 0x94, 0xef, 0xbd, 0xcf, 0x33, 0x6f, 0xbc, 0xf0, 0x21, 0x2f, 0xdc, 0xa9, + 0x49, 0x23, 0xe4, 0x2a, 0xce, 0x99, 0xf3, 0x92, 0x62, 0x2c, 0xb9, 0xc9, 0x6a, 0xc3, 0x29, 0x99, + 0x18, 0xb9, 0xaa, 0x58, 0xc7, 0x9c, 0x34, 0xee, 0x14, 0xd7, 0x86, 0x1d, 0xc7, 0xc8, 0xfa, 0x58, + 0xe4, 0x91, 0x17, 0x62, 0xf1, 0x08, 0x8e, 0x3c, 0xb5, 0xfe, 0x1d, 0xc0, 0x64, 0xef, 0x19, 0xb1, + 0x87, 0xa7, 0x29, 0x25, 0x86, 0x8c, 0x72, 0xfc, 0x9d, 0x74, 0x18, 0xac, 0x82, 0xcd, 0xfc, 0xcd, + 0xcb, 0xe8, 0xea, 0xa3, 0x68, 0xe7, 0xb1, 0x2f, 0x2d, 0x75, 0x18, 0xc8, 0x79, 0x7a, 0x91, 0xe2, + 0x2b, 0x88, 0xae, 0x98,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xb7, 0x3e, 0xe8, 0xfe, 0xc9, 0x73, 0x98, + 0x26, 0x4d, 0x56, 0x90, 0x46, 0xea, 0xce, 0x93, 0x7f, 0xf5, 0x9f, 0x00, 0x00, 0x00, 0xff, 0xff, + 0xe2, 0x44, 0x04, 0xd1, 0x41, 0x02, 0x00, 0x00, } diff --git a/common/oauth/proto/config.proto b/common/oauth/proto/config.proto index 756ec54c..90c8b369 100644 --- a/common/oauth/proto/config.proto +++ b/common/oauth/proto/config.proto @@ -35,4 +35,11 @@ message BearerToken { message GoogleCredentials { optional string json_file = 1; repeated string scope = 2; + + // Use encoded JWT directly as access token, instead of implementing the whole + // OAuth2.0 flow. + optional bool jwt_as_access_token = 4; + + // Audience works only if jwt_as_access_token is true. + optional string audience = 3; }