consul/agent/connect/ca/provider_vault_auth_gcp.go

48 lines
1.4 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package ca
import (
"fmt"
"github.com/hashicorp/vault/api/auth/gcp"
"github.com/hashicorp/consul/agent/structs"
)
var _ VaultAuthenticator = (*gcp.GCPAuth)(nil)
// NewGCPAuthClient returns a VaultAuthenticator that can log into Vault using the GCP auth method.
func NewGCPAuthClient(authMethod *structs.VaultAuthMethod) (VaultAuthenticator, error) {
// Check if the configuration already contains a JWT auth token. If so we want to
// perform a direct request to the login API with the config that is provided.
// This supports the Vault CA config in a backwards compatible way so that we don't
// break existing configurations.
if legacyCheck(authMethod.Params, "jwt") {
return NewVaultAPIAuthClient(authMethod, ""), nil
}
// Create a GCPAuth client from the CA config
params, err := toMapStringString(authMethod.Params)
if err != nil {
return nil, fmt.Errorf("misconfiguration of GCP auth parameters: %w", err)
}
opts := []gcp.LoginOption{gcp.WithMountPath(authMethod.MountPath)}
// Handle the login type explicitly.
switch params["type"] {
case "gce":
opts = append(opts, gcp.WithGCEAuth())
default:
opts = append(opts, gcp.WithIAMAuth(params["service_account_email"]))
}
auth, err := gcp.NewGCPAuth(params["role"], opts...)
if err != nil {
return nil, fmt.Errorf("failed to create a new Vault GCP auth client: %w", err)
}
return auth, nil
}