Skip to content
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

feat: accept base64 encoded credentials in policy.IAM config #61

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions plugins/identities/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package identities

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -36,8 +37,11 @@ type HTTPAuthConfig struct {

// google_idtoken
Audience string `mapstructure:"audience,omitempty" json:"audience,omitempty" yaml:"audience,omitempty" validate:"required_if=Type google_idtoken"`
// TODO: allow base64 encoded credentials
CredentialsJSON string `mapstructure:"credentials_json,omitempty" json:"credentials_json,omitempty" yaml:"credentials_json,omitempty" validate:"required_if=Type google_idtoken"`
// CredentialsJSON accept a JSON stringified credentials
// Deprecated: CredentialsJSON is deprecated, use CredentialsJSONBase64 instead
CredentialsJSON string `mapstructure:"credentials_json,omitempty" json:"credentials_json,omitempty" yaml:"credentials_json,omitempty"`
// CredentialsJSONBase64 accept a base64 encoded JSON stringified credentials
CredentialsJSONBase64 string `mapstructure:"credentials_json_base64,omitempty" json:"credentials_json_base64,omitempty" yaml:"credentials_json_base64,omitempty"`
}

// HTTPClientConfig is the configuration required by iam.Client
Expand Down Expand Up @@ -88,6 +92,13 @@ func (c *HTTPClientConfig) Encrypt() error {
}
c.Auth.CredentialsJSON = encryptedValue
}
if c.Auth.CredentialsJSONBase64 != "" {
encryptedValue, err := c.crypto.Encrypt(c.Auth.CredentialsJSONBase64)
if err != nil {
return err
}
c.Auth.CredentialsJSONBase64 = encryptedValue
}
}

return nil
Expand Down Expand Up @@ -126,6 +137,13 @@ func (c *HTTPClientConfig) Decrypt() error {
}
c.Auth.CredentialsJSON = decryptedValue
}
if c.Auth.CredentialsJSONBase64 != "" {
decryptedValue, err := c.crypto.Decrypt(c.Auth.CredentialsJSONBase64)
mabdh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
c.Auth.CredentialsJSONBase64 = decryptedValue
}
}

return nil
Expand All @@ -151,8 +169,22 @@ func NewHTTPClient(config *HTTPClientConfig) (*HTTPClient, error) {
}

if config.Auth.Type == "google_idtoken" {
var creds []byte
switch {
case config.Auth.CredentialsJSONBase64 != "":
v, err := base64.StdEncoding.DecodeString(config.Auth.CredentialsJSONBase64)
if err != nil {
return nil, fmt.Errorf("decoding credentials_json_base64: %w", err)
}
creds = v
case config.Auth.CredentialsJSON != "":
creds = []byte(config.Auth.CredentialsJSON)
default:
return nil, fmt.Errorf("missing credentials for google_idtoken auth")
}

ctx := context.Background()
ts, err := idtoken.NewTokenSource(ctx, config.Auth.Audience, idtoken.WithCredentialsJSON([]byte(config.Auth.CredentialsJSON)))
ts, err := idtoken.NewTokenSource(ctx, config.Auth.Audience, idtoken.WithCredentialsJSON(creds))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -236,3 +268,8 @@ func (c *HTTPClient) setAuth(req *http.Request) {
}
}
}

func isValidJSON(s string) bool {
var v map[string]interface{}
return json.Unmarshal([]byte(s), &v) == nil
}