stacks: load credentials from config file on startup (#35952)

* stacks: load credentials from config file on startup

* delete unneeded file
This commit is contained in:
Liam Cervante 2024-11-05 16:13:08 +01:00 committed by GitHub
parent f0b00c45f7
commit 7c4aeac5f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 26 additions and 50 deletions

View file

@ -91,7 +91,7 @@ func initCommands(
View: views.NewView(streams).SetRunningInAutomation(inAutomation),
Color: true,
GlobalPluginDirs: globalPluginDirs(),
GlobalPluginDirs: cliconfig.GlobalPluginDirs(),
Ui: Ui,
Services: services,
@ -475,6 +475,6 @@ func makeShutdownCh() <-chan struct{} {
}
func credentialsSource(config *cliconfig.Config) (auth.CredentialsSource, error) {
helperPlugins := pluginDiscovery.FindPlugins("credentials", globalPluginDirs())
helperPlugins := pluginDiscovery.FindPlugins("credentials", cliconfig.GlobalPluginDirs())
return config.CredentialsSource(helperPlugins)
}

View file

@ -1,27 +1,25 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package main
package cliconfig
import (
"fmt"
"log"
"path/filepath"
"runtime"
"github.com/hashicorp/terraform/internal/command/cliconfig"
)
// globalPluginDirs returns directories that should be searched for
// GlobalPluginDirs returns directories that should be searched for
// globally-installed plugins (not specific to the current configuration).
//
// Earlier entries in this slice get priority over later when multiple copies
// of the same plugin version are found, but newer versions always override
// older versions where both satisfy the provider version constraints.
func globalPluginDirs() []string {
func GlobalPluginDirs() []string {
var ret []string
// Look in ~/.terraform.d/plugins/ , or its equivalent on non-UNIX
dir, err := cliconfig.ConfigDir()
dir, err := ConfigDir()
if err != nil {
log.Printf("[ERROR] Error finding global config directory: %s", err)
} else {

View file

@ -1,40 +0,0 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package rpcapi
import (
svchost "github.com/hashicorp/terraform-svchost"
"github.com/hashicorp/terraform-svchost/auth"
"github.com/zclconf/go-cty/cty"
)
var _ auth.CredentialsSource = &credentialsSource{}
type credentialsSource struct {
configured map[svchost.Hostname]cty.Value
}
func newCredentialsSource() *credentialsSource {
return &credentialsSource{
configured: map[svchost.Hostname]cty.Value{},
}
}
func (c *credentialsSource) ForHost(host svchost.Hostname) (auth.HostCredentials, error) {
v, ok := c.configured[host]
if ok {
return auth.HostCredentialsFromObject(v), nil
}
return nil, nil
}
func (c *credentialsSource) StoreForHost(host svchost.Hostname, credentials auth.HostCredentialsWritable) error {
c.configured[host] = credentials.ToStore()
return nil
}
func (c *credentialsSource) ForgetForHost(host svchost.Hostname) error {
delete(c.configured, host)
return nil
}

View file

@ -13,6 +13,8 @@ import (
"github.com/hashicorp/terraform-svchost/disco"
"google.golang.org/grpc"
"github.com/hashicorp/terraform/internal/command/cliconfig"
pluginDiscovery "github.com/hashicorp/terraform/internal/plugin/discovery"
"github.com/hashicorp/terraform/internal/rpcapi/dynrpcserver"
"github.com/hashicorp/terraform/internal/rpcapi/terraform1/dependencies"
"github.com/hashicorp/terraform/internal/rpcapi/terraform1/packages"
@ -101,9 +103,25 @@ type serviceOpts struct {
}
func newServiceDisco(config *setup.Config) (*disco.Disco, error) {
services := disco.New()
credSrc := newCredentialsSource()
// First, we'll try and load any credentials that might have been available
// to the UI. It's perfectly fine if there are none so any errors we find
// are from malformed credentials rather than missing ones.
file, diags := cliconfig.LoadConfig()
if diags.HasErrors() {
return nil, fmt.Errorf("problem loading CLI configuration: %w", diags.ErrWithWarnings())
}
helperPlugins := pluginDiscovery.FindPlugins("credentials", cliconfig.GlobalPluginDirs())
src, err := file.CredentialsSource(helperPlugins)
if err != nil {
return nil, fmt.Errorf("problem creating credentials source: %w", err)
}
services := disco.NewWithCredentialsSource(src)
// Second, we'll side-load any credentials that might have been passed in.
credSrc := services.CredentialsSource()
if config != nil {
for host, cred := range config.GetCredentials() {
if err := credSrc.StoreForHost(svchost.Hostname(host), auth.HostCredentialsToken(cred.Token)); err != nil {