forked from Azure/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Get restConfig supporting MSI and secret flow (Azure#14)
feat: Get hub restConfig from querying AAD TODO: refactor the code to support provider pattern
- Loading branch information
Showing
6 changed files
with
146 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright (c) Microsoft Corporation. | ||
Licensed under the MIT license. | ||
*/ | ||
package configprovider | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"go.goms.io/fleet/pkg/interfaces" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
const ( | ||
aksScope = "6dae42f8-4368-4678-94ff-3960e28e3630" | ||
) | ||
|
||
type azureAuthProvider struct{} | ||
|
||
func NewAzureAuthProvider() interfaces.AuthenticationProvider { | ||
return &azureAuthProvider{} | ||
} | ||
|
||
//GetConfig returns a RESTConfig which could be used to make request to the associated fleet' hub cluster. | ||
func (obj *azureAuthProvider) GetConfig(ctx context.Context, hubServerAPIAddress string) (rest.Config, error) { | ||
clusterIdentity := os.Getenv("cluster_identity") | ||
|
||
if clusterIdentity == "" { | ||
return rest.Config{}, errors.New("cluster identity empty") | ||
} | ||
|
||
if hubServerAPIAddress == "" { | ||
return rest.Config{}, errors.New("hub server api address empty") | ||
} | ||
|
||
opts := &azidentity.ManagedIdentityCredentialOptions{ID: azidentity.ClientID(clusterIdentity)} | ||
credential, err := azidentity.NewManagedIdentityCredential(opts) | ||
|
||
if err != nil { | ||
return rest.Config{}, err | ||
} | ||
|
||
token, err := credential.GetToken(ctx, policy.TokenRequestOptions{ | ||
Scopes: []string{aksScope}, | ||
}) | ||
|
||
if err != nil { | ||
return rest.Config{}, err | ||
} | ||
|
||
emptyToken := azcore.AccessToken{} | ||
if *(token) == emptyToken { | ||
return rest.Config{}, errors.New("access token empty") | ||
} | ||
|
||
return rest.Config{ | ||
BearerToken: token.Token, | ||
Host: hubServerAPIAddress, | ||
TLSClientConfig: rest.TLSClientConfig{ | ||
Insecure: true, // TODO (mng): Remove this line once server CA can be passed in as flag. | ||
}, | ||
}, nil | ||
} |
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
21 changes: 21 additions & 0 deletions
21
pkg/controllers/memberinternalmembercluster/utils/config.go
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func GetConfigWithSecret(secret v1.Secret) (rest.Config, error) { | ||
kubeConfig, ok := secret.Data["kubeconfig"] | ||
if !ok || len(kubeConfig) == 0 { | ||
return rest.Config{}, errors.New("secret does not contain kubeconfig") | ||
} | ||
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig) | ||
if err != nil { | ||
return rest.Config{}, err | ||
} | ||
|
||
return *(restConfig), nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type AuthenticationProvider interface { | ||
GetConfig(ctx context.Context, hubServerAPIAddress string) (rest.Config, error) | ||
} |