-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from snootan/feat/add-governor-agent
Add governor api call provision to send cluster telemetry data
- Loading branch information
Showing
37 changed files
with
5,982 additions
and
20 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
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
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,116 @@ | ||
package cspauth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/vmware-tanzu/cloud-native-security-inspector/src/lib/log" | ||
"github.com/vmware-tanzu/cloud-native-security-inspector/src/lib/retry" | ||
v12 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"math" | ||
"time" | ||
) | ||
|
||
const ( | ||
tokenMaxAgeSeconds = 1700 | ||
apiToken = "API_TOKEN" | ||
accessTokenSecretName = "governor-accesstoken" | ||
governorTokenExpiresIn = "governorAccessTokenExpiresIn" | ||
governorAccessTokenKey = "governorAccessToken" | ||
Retry = 3 | ||
) | ||
|
||
var RetryDelay time.Duration = 5 | ||
|
||
// Provider is an interface to interact with an authorization service | ||
type Provider interface { | ||
// GetBearerToken retrieves a short-lived access token to use in a single HTTP request | ||
GetBearerToken(kubernetes.Interface, context.Context, string, string) (string, error) | ||
} | ||
|
||
type CspAuth struct { | ||
CspClient CSPClient | ||
|
||
apiToken string | ||
} | ||
|
||
func (a *CspAuth) GetBearerToken(clientSet kubernetes.Interface, ctx context.Context, cspSecretNamespace string, cspSecretName string) (string, error) { | ||
accessSecret, err := getOrCreateSecretForAccessToken(clientSet, ctx, cspSecretNamespace) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
accessToken := string(accessSecret.Data[governorAccessTokenKey]) | ||
expiresIn := string(accessSecret.Data[governorTokenExpiresIn]) | ||
accessTokenExpiresIn, _ := time.Parse(time.Layout, expiresIn) | ||
|
||
if accessToken == "" || time.Now().After(accessTokenExpiresIn) { | ||
apiToken, err := getCSPTokenFromSecret(clientSet, ctx, cspSecretNamespace, cspSecretName) | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to fetch CSP api-token: %w", err) | ||
} | ||
a.apiToken = apiToken | ||
if err := a.refreshToken(ctx, clientSet, cspSecretNamespace, accessSecret); err != nil { | ||
return "", err | ||
} | ||
} | ||
return string(accessSecret.Data[governorAccessTokenKey]), nil | ||
} | ||
|
||
func (a *CspAuth) refreshToken(ctx context.Context, clientSet kubernetes.Interface, cspSecretNamespace string, accessTokenSecret *v12.Secret) error { | ||
return retry.NewRetry( | ||
retry.WithName("auth token refresh"), | ||
retry.WithMaxAttempts(Retry), | ||
retry.WithIncrementDelay(RetryDelay*time.Second, RetryDelay*time.Second), | ||
).Run(ctx, func() (bool, error) { | ||
now := time.Now() | ||
cspAuthResponse, err := a.CspClient.GetCspAuthorization(ctx, a.apiToken) | ||
if err != nil { | ||
log.Error(err, "We got an error back from CSP") | ||
return false, nil | ||
} | ||
|
||
expiresIn := time.Duration(math.Min(float64(cspAuthResponse.ExpiresIn), tokenMaxAgeSeconds)) * time.Second | ||
formattedExpiration := now.Add(expiresIn).Format(time.Layout) | ||
|
||
log.Infof("Refreshed access token for governor which expires in %s", formattedExpiration) | ||
accessTokenSecret.Data = make(map[string][]byte, 0) | ||
accessTokenSecret.Data[governorAccessTokenKey] = []byte(cspAuthResponse.AccessToken) | ||
accessTokenSecret.Data[governorTokenExpiresIn] = []byte(formattedExpiration) | ||
_, err = clientSet.CoreV1().Secrets(cspSecretNamespace).Update(ctx, accessTokenSecret, v1.UpdateOptions{}) | ||
if err != nil { | ||
log.Error(err, "We got an error updating access token secret") | ||
return false, nil | ||
} | ||
log.Infof("Obtained CSP access token, next refresh in %s\n", expiresIn) | ||
return true, nil | ||
}) | ||
} | ||
|
||
func getCSPTokenFromSecret(clientSet kubernetes.Interface, ctx context.Context, ns string, secretName string) (string, error) { | ||
secret, err := clientSet.CoreV1().Secrets(ns).Get(ctx, secretName, v1.GetOptions{}) | ||
if err != nil { | ||
log.Error(err, "Failed to fetch secret") | ||
return "", err | ||
} | ||
cspApiToken := string(secret.Data[apiToken]) | ||
return cspApiToken, err | ||
} | ||
|
||
func getOrCreateSecretForAccessToken(clientSet kubernetes.Interface, ctx context.Context, ns string) (*v12.Secret, error) { | ||
secret, err := clientSet.CoreV1().Secrets(ns).Get(ctx, accessTokenSecretName, v1.GetOptions{}) | ||
if err != nil { | ||
log.Warning(err, "Failed to fetch secret for access token, Now Trying to create new secret for same") | ||
secret = &v12.Secret{} | ||
secret.Name = accessTokenSecretName | ||
secret.Namespace = ns | ||
secret.Data = make(map[string][]byte, 0) | ||
secret, err = clientSet.CoreV1().Secrets(ns).Create(ctx, secret, v1.CreateOptions{}) | ||
if err != nil { | ||
log.Error(err, "Failed to create secret for storing access token.") | ||
return nil, err | ||
} | ||
} | ||
return secret, err | ||
} |
Oops, something went wrong.