forked from kedacore/keda
-
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(scaler/prometheus): support authentication for Google Managed Pr…
…ometheus (kedacore#4675)
- Loading branch information
1 parent
2234a6f
commit b8a8d57
Showing
9 changed files
with
372 additions
and
33 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 |
---|---|---|
@@ -1,37 +1,90 @@ | ||
package scalers | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"errors" | ||
"net/http" | ||
"os" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
|
||
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" | ||
) | ||
|
||
var ( | ||
gcpScopeMonitoringRead = "https://www.googleapis.com/auth/monitoring.read" | ||
|
||
errGoogleApplicationCrendentialsNotFound = errors.New("google application credentials not found") | ||
) | ||
|
||
type gcpAuthorizationMetadata struct { | ||
GoogleApplicationCredentials string | ||
GoogleApplicationCredentialsFile string | ||
podIdentityProviderEnabled bool | ||
} | ||
|
||
func getGcpAuthorization(config *ScalerConfig, resolvedEnv map[string]string) (*gcpAuthorizationMetadata, error) { | ||
metadata := config.TriggerMetadata | ||
authParams := config.AuthParams | ||
meta := gcpAuthorizationMetadata{} | ||
|
||
switch { | ||
case config.PodIdentity.Provider == kedav1alpha1.PodIdentityProviderGCP: | ||
// do nothing, rely on underneath metadata google | ||
meta.podIdentityProviderEnabled = true | ||
case authParams["GoogleApplicationCredentials"] != "": | ||
meta.GoogleApplicationCredentials = authParams["GoogleApplicationCredentials"] | ||
default: | ||
switch { | ||
case metadata["credentialsFromEnv"] != "": | ||
meta.GoogleApplicationCredentials = resolvedEnv[metadata["credentialsFromEnv"]] | ||
case metadata["credentialsFromEnvFile"] != "": | ||
meta.GoogleApplicationCredentialsFile = resolvedEnv[metadata["credentialsFromEnvFile"]] | ||
default: | ||
return nil, fmt.Errorf("GoogleApplicationCredentials not found") | ||
func (a *gcpAuthorizationMetadata) tokenSource(ctx context.Context, scopes ...string) (oauth2.TokenSource, error) { | ||
if a.podIdentityProviderEnabled { | ||
return google.DefaultTokenSource(ctx, scopes...) | ||
} | ||
|
||
if a.GoogleApplicationCredentials != "" { | ||
creds, err := google.CredentialsFromJSON(ctx, []byte(a.GoogleApplicationCredentials), scopes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return creds.TokenSource, nil | ||
} | ||
|
||
if a.GoogleApplicationCredentialsFile != "" { | ||
data, err := os.ReadFile(a.GoogleApplicationCredentialsFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
creds, err := google.CredentialsFromJSON(ctx, data, scopes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return creds.TokenSource, nil | ||
} | ||
|
||
return nil, errGoogleApplicationCrendentialsNotFound | ||
} | ||
|
||
func getGCPAuthorization(config *ScalerConfig) (*gcpAuthorizationMetadata, error) { | ||
if config.PodIdentity.Provider == kedav1alpha1.PodIdentityProviderGCP { | ||
return &gcpAuthorizationMetadata{podIdentityProviderEnabled: true}, nil | ||
} | ||
|
||
if creds := config.AuthParams["GoogleApplicationCredentials"]; creds != "" { | ||
return &gcpAuthorizationMetadata{GoogleApplicationCredentials: creds}, nil | ||
} | ||
|
||
if creds := config.TriggerMetadata["credentialsFromEnv"]; creds != "" { | ||
return &gcpAuthorizationMetadata{GoogleApplicationCredentials: config.ResolvedEnv[creds]}, nil | ||
} | ||
|
||
if credsFile := config.TriggerMetadata["credentialsFromEnvFile"]; credsFile != "" { | ||
return &gcpAuthorizationMetadata{GoogleApplicationCredentialsFile: config.ResolvedEnv[credsFile]}, nil | ||
} | ||
return &meta, nil | ||
|
||
return nil, errGoogleApplicationCrendentialsNotFound | ||
} | ||
|
||
func getGCPOAuth2HTTPTransport(config *ScalerConfig, base http.RoundTripper, scopes ...string) (http.RoundTripper, error) { | ||
a, err := getGCPAuthorization(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ts, err := a.tokenSource(context.Background(), scopes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &oauth2.Transport{Source: ts, Base: base}, 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
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
Oops, something went wrong.