-
Notifications
You must be signed in to change notification settings - Fork 2.1k
GCP CLI support: server-side request handler #19789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8ad4596
GCP CLI support: API changes
Tener cdc972d
GCP CLI support: RBAC changes
Tener c521c1a
Review: perform case-insensitive match, add tests, add docs.
Tener f42979b
GCP CLI support: CA changes
Tener f68410f
Review: improve docs.
Tener a5ce48d
Review: simplify test.
Tener 6167ed7
Review: use plural form `GCPServiceAccountsASN1ExtensionOID`
Tener 2e7c71e
GCP CLI support: auth changes
Tener f2b1b4a
GCP CLI support: server-side request handler
Tener ca7b4ad
Review: use specialized functions in tests.
Tener d108bb8
Review: improve GCP handler.
Tener 46d1ddc
Review: improve Azure handler.
Tener f86fbd7
Merge branch 'master' into tener/gcloud-cli-handler
Tener 912e036
Merge branch 'master' into tener/gcloud-cli-handler
Tener 35f1336
Merge branch 'master' into tener/gcloud-cli-handler
Tener File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2022 Gravitational, 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 gcp | ||
|
|
||
| import ( | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| // gcpEndpointSuffix is hostname suffix used to determine if hostname is a GCP endpoint | ||
| gcpEndpointSuffix = ".googleapis.com" | ||
| ) | ||
|
|
||
| // IsGCPEndpoint returns true if hostname is a GCP endpoint | ||
| func IsGCPEndpoint(hostname string) bool { | ||
| return strings.HasSuffix(hostname, gcpEndpointSuffix) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2022 Gravitational, 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 gcp | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIsGCPEndpoint(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| hostname string | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "compute googleapis", | ||
| hostname: "compute.googleapis.com", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "top level googleapis", | ||
| hostname: "googleapis.com", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "localhost", | ||
| hostname: "localhost", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "fake googleapis", | ||
| hostname: "compute.googleapis.com.fake.com", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "top level-like fake googleapis", | ||
| hostname: "googleapis.com.fake.com", | ||
| want: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| require.Equal(t, tt.want, IsGCPEndpoint(tt.hostname)) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Copyright 2022 Gravitational, 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 azure | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| "github.com/gravitational/trace" | ||
| "github.com/jonboulle/clockwork" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestForwarder_getToken(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| type testCase struct { | ||
| name string | ||
|
|
||
| config HandlerConfig | ||
|
|
||
| managedIdentity string | ||
| scope string | ||
|
|
||
| wantToken *azcore.AccessToken | ||
| checkErr require.ErrorAssertionFunc | ||
| } | ||
|
|
||
| var tests []testCase | ||
|
|
||
| tests = []testCase{ | ||
| { | ||
| name: "base case", | ||
| config: HandlerConfig{ | ||
| getAccessToken: func(ctx context.Context, managedIdentity string, scope string) (*azcore.AccessToken, error) { | ||
| if managedIdentity != "MY_IDENTITY" { | ||
| return nil, trace.BadParameter("wrong managedIdentity") | ||
| } | ||
| if scope != "MY_SCOPE" { | ||
| return nil, trace.BadParameter("wrong scope") | ||
| } | ||
| return &azcore.AccessToken{Token: "foobar"}, nil | ||
| }, | ||
| }, | ||
| managedIdentity: "MY_IDENTITY", | ||
| scope: "MY_SCOPE", | ||
| wantToken: &azcore.AccessToken{Token: "foobar"}, | ||
| checkErr: require.NoError, | ||
| }, | ||
| { | ||
| name: "timeout", | ||
| config: HandlerConfig{ | ||
| Clock: clockwork.NewFakeClock(), | ||
| getAccessToken: func(ctx context.Context, managedIdentity string, scope string) (*azcore.AccessToken, error) { | ||
| // find the fake clock from above | ||
| var clock clockwork.FakeClock | ||
| for _, test := range tests { | ||
| if test.name == "timeout" { | ||
| clock = test.config.Clock.(clockwork.FakeClock) | ||
| } | ||
| } | ||
|
|
||
| clock.Advance(getTokenTimeout) | ||
| clock.Sleep(getTokenTimeout * 2) | ||
| return &azcore.AccessToken{Token: "foobar"}, nil | ||
| }, | ||
| }, | ||
| checkErr: func(t require.TestingT, err error, i ...interface{}) { | ||
| require.ErrorContains(t, err, "timeout waiting for access token for 5s") | ||
| require.ErrorIs(t, err, context.DeadlineExceeded) | ||
| }, | ||
| }, | ||
| { | ||
| name: "non-timeout error", | ||
| config: HandlerConfig{ | ||
| getAccessToken: func(ctx context.Context, managedIdentity string, scope string) (*azcore.AccessToken, error) { | ||
| return nil, trace.BadParameter("bad param foo") | ||
| }, | ||
| }, | ||
| checkErr: func(t require.TestingT, err error, i ...interface{}) { | ||
| require.ErrorContains(t, err, "bad param foo") | ||
| require.True(t, trace.IsBadParameter(err)) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| hnd, err := newAzureHandler(ctx, tt.config) | ||
| require.NoError(t, err) | ||
|
|
||
| token, err := hnd.getToken(ctx, tt.managedIdentity, tt.scope) | ||
|
|
||
| require.Equal(t, tt.wantToken, token) | ||
| tt.checkErr(t, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestForwarder_getToken_cache(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| clock := clockwork.NewFakeClock() | ||
|
|
||
| calls := 0 | ||
| hnd, err := newAzureHandler(ctx, HandlerConfig{ | ||
| Clock: clock, | ||
| getAccessToken: func(ctx context.Context, managedIdentity string, scope string) (*azcore.AccessToken, error) { | ||
| calls++ | ||
| return &azcore.AccessToken{Token: "OK"}, nil | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| // first call goes through | ||
| _, err = hnd.getToken(ctx, "", "") | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, calls) | ||
|
|
||
| // second call is cached | ||
| _, err = hnd.getToken(ctx, "", "") | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, calls) | ||
|
|
||
| // advance past cache expiry | ||
| clock.Advance(time.Second * 60 * 2) | ||
|
|
||
| // third call goes through | ||
| _, err = hnd.getToken(ctx, "", "") | ||
| require.NoError(t, err) | ||
| require.Equal(t, 2, calls) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.