forked from raystack/meteor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Replaced use on Access token in merlin extractor with Idtoken (#50
) Co-authored-by: Mayur Jagtap <[email protected]>
- Loading branch information
1 parent
4bf1b95
commit d0af130
Showing
2 changed files
with
30 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,20 +20,23 @@ import ( | |
var ( | ||
ctx = context.Background() | ||
credsJSON = []byte(`{"type":"service_account","project_id":"company-data-platform","private_key_id":"698vxv308w3i68p938040bz817r95b1e0k4kmvqs","private_key":"-----BEGIN PRIVATE KEY-----\nMIIEoQIBAAKCAQBVzQ0WPuaqdwMNapCGKdKUR/MOgWNByruT60SJwd5lY/2Sjx1w\nQ4sJ6xk/+Tz7bT3CgNBAPQ+rZfLD2fdQJIBeYElRcHw6a2PA/6TaX2e4qq0+5xk3\ngnItlqZm0hQElZd76LNlMcItHNmneLICowTOdzl0hUd2IgrqLB545v3KOGfwoEAp\nz3mPm/iF1+zTPWy041w7ajvWK2N3mRygKoP79ne2gDuN2+QHmW8wPFzQ3pdQZU65\n1npgP9N4wRHQT8vowTUSYdSRZG1p1MPKCXsrqhMUv7yLdrOcacAvcZqYOcMhJQ1p\nbYpsinDR65ARDduMeKoEUkFb3hf2zPUcdYNhAgMBAAECggEAQldOxCGUlr94o7n+\nz02tHavYGiIfDfLkQIYLs3wsKjc7DEQOHgyLh/q4xkc/SKR5uVeCLflIkV09bQOu\nftAKVW6bohWYaE86jTLdU1+rQhTt6ZIkZFA/WlJ+jUfn5HeJ7mvJsffcTKde/2eK\nNBG6GK4Exbx7ubKuv8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DlUvfgSNTj+g==\n-----END PRIVATE KEY-----\n","client_email":"[email protected]","client_id":"043161688880430795893","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/systems-meteor%40company-data-platform.iam.gserviceaccount.com"}`) | ||
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
params ClientParams | ||
errStr string | ||
ctx context.Context | ||
}{ | ||
{ | ||
name: "Valid", | ||
params: ClientParams{ | ||
BaseURL: "http://company.com/api/merlin/", | ||
ServiceAccountJSON: credsJSON, | ||
}, | ||
ctx: ctxWithClientWithIDToken(t, token), | ||
}, | ||
{ | ||
name: "WithoutCredentials", | ||
|
@@ -55,11 +58,12 @@ func TestNewClient(t *testing.T) { | |
ServiceAccountJSON: credsJSON, | ||
}, | ||
errStr: `invalid input: parse "http://Gintama - Yorozuya Gin-chan": invalid character " " in host name`, | ||
ctx: ctxWithClientWithIDToken(t, token), | ||
}, | ||
} | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
_, err := NewClient(ctx, tc.params) | ||
_, err := NewClient(tc.ctx, tc.params) | ||
if tc.errStr != "" { | ||
assert.ErrorContains(t, err, tc.errStr) | ||
} else { | ||
|
@@ -147,7 +151,6 @@ func TestProjects(t *testing.T) { | |
} | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
token := "MyIncrediblyPowerfulAccessToken" | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet) | ||
assert.Equal(t, r.URL.Path, "/api/merlin/v1/projects") | ||
|
@@ -157,7 +160,7 @@ func TestProjects(t *testing.T) { | |
})) | ||
defer srv.Close() | ||
|
||
c, err := NewClient(ctxWithClient(t, token), ClientParams{ | ||
c, err := NewClient(ctxWithClientWithIDToken(t, token), ClientParams{ | ||
BaseURL: srv.URL + "/api/merlin", | ||
ServiceAccountJSON: credsJSON, | ||
Timeout: 1 * time.Second, | ||
|
@@ -183,7 +186,7 @@ func TestProjects(t *testing.T) { | |
})) | ||
defer srv.Close() | ||
|
||
c, err := NewClient(ctxWithClient(t, "MyIncrediblyPowerfulAccessToken"), ClientParams{ | ||
c, err := NewClient(ctxWithClientWithIDToken(t, token), ClientParams{ | ||
BaseURL: srv.URL + "/api/merlin", | ||
ServiceAccountJSON: credsJSON, | ||
Timeout: timeout, | ||
|
@@ -346,7 +349,6 @@ func TestModels(t *testing.T) { | |
} | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
token := "MyIncrediblyPowerfulAccessToken" | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet) | ||
assert.Equal(t, r.URL.Path, fmt.Sprintf("/api/merlin/v1/projects/%d/models", tc.projectID)) | ||
|
@@ -356,7 +358,7 @@ func TestModels(t *testing.T) { | |
})) | ||
defer srv.Close() | ||
|
||
c, err := NewClient(ctxWithClient(t, token), ClientParams{ | ||
c, err := NewClient(ctxWithClientWithIDToken(t, token), ClientParams{ | ||
BaseURL: srv.URL + "/api/merlin", | ||
ServiceAccountJSON: credsJSON, | ||
Timeout: 1 * time.Second, | ||
|
@@ -382,7 +384,7 @@ func TestModels(t *testing.T) { | |
})) | ||
defer srv.Close() | ||
|
||
c, err := NewClient(ctxWithClient(t, "MyIncrediblyPowerfulAccessToken"), ClientParams{ | ||
c, err := NewClient(ctxWithClientWithIDToken(t, token), ClientParams{ | ||
BaseURL: srv.URL + "/api/merlin", | ||
ServiceAccountJSON: credsJSON, | ||
Timeout: timeout, | ||
|
@@ -467,7 +469,6 @@ func TestModelVersion(t *testing.T) { | |
} | ||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
token := "MyIncrediblyPowerfulAccessToken" | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet) | ||
assert.Equal(t, r.URL.Path, fmt.Sprintf( | ||
|
@@ -479,7 +480,7 @@ func TestModelVersion(t *testing.T) { | |
})) | ||
defer srv.Close() | ||
|
||
c, err := NewClient(ctxWithClient(t, token), ClientParams{ | ||
c, err := NewClient(ctxWithClientWithIDToken(t, token), ClientParams{ | ||
BaseURL: srv.URL + "/api/merlin", | ||
ServiceAccountJSON: credsJSON, | ||
Timeout: 1 * time.Second, | ||
|
@@ -505,7 +506,7 @@ func TestModelVersion(t *testing.T) { | |
})) | ||
defer srv.Close() | ||
|
||
c, err := NewClient(ctxWithClient(t, "MyIncrediblyPowerfulAccessToken"), ClientParams{ | ||
c, err := NewClient(ctxWithClientWithIDToken(t, token), ClientParams{ | ||
BaseURL: srv.URL + "/api/merlin", | ||
ServiceAccountJSON: credsJSON, | ||
Timeout: timeout, | ||
|
@@ -517,23 +518,23 @@ func TestModelVersion(t *testing.T) { | |
}) | ||
} | ||
|
||
func ctxWithClient(t *testing.T, token string) context.Context { | ||
func ctxWithClientWithIDToken(t *testing.T, token string) context.Context { | ||
return context.WithValue(ctx, oauth2.HTTPClient, &http.Client{ | ||
Transport: mockOauthRoundTripper{ | ||
T: t, | ||
AccessToken: token, | ||
Base: http.DefaultTransport, | ||
Transport: mockIDTokenRoundTripper{ | ||
T: t, | ||
IDToken: token, | ||
Base: http.DefaultTransport, | ||
}, | ||
}) | ||
} | ||
|
||
type mockOauthRoundTripper struct { | ||
T *testing.T | ||
AccessToken string | ||
Base http.RoundTripper | ||
type mockIDTokenRoundTripper struct { | ||
T *testing.T | ||
IDToken string | ||
Base http.RoundTripper | ||
} | ||
|
||
func (m mockOauthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
func (m mockIDTokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if m.match(req) { | ||
return &http.Response{ | ||
Status: http.StatusText(http.StatusOK), | ||
|
@@ -543,9 +544,7 @@ func (m mockOauthRoundTripper) RoundTrip(req *http.Request) (*http.Response, err | |
ProtoMinor: 1, | ||
Header: make(http.Header), | ||
Body: testutils.ValueAsJSONReader(m.T, map[string]interface{}{ | ||
"access_token": m.AccessToken, | ||
"expires_in": 3599, | ||
"token_type": "Bearer", | ||
"id_token": m.IDToken, | ||
}), | ||
Uncompressed: true, | ||
}, nil | ||
|
@@ -554,7 +553,7 @@ func (m mockOauthRoundTripper) RoundTrip(req *http.Request) (*http.Response, err | |
return m.Base.RoundTrip(req) | ||
} | ||
|
||
func (m mockOauthRoundTripper) match(r *http.Request) bool { | ||
func (m mockIDTokenRoundTripper) match(r *http.Request) bool { | ||
return r.Method == http.MethodPost && | ||
r.URL.Host == "oauth2.googleapis.com" && | ||
r.URL.Path == "/token" | ||
|