Merge pull request #1216 from cbowman0/tfe

Support passing in the Terraform API URL to support Terraform Enterprise installations.
main
Elie 2021-11-15 17:40:32 +01:00 committed by GitHub
commit 0bf122f7ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 17 deletions

View File

@ -168,6 +168,12 @@ func NewScanCmd(opts *pkg.ScanOptions) *cobra.Command {
"Terraform Cloud / Enterprise API token.\n"+ "Terraform Cloud / Enterprise API token.\n"+
"Only used with tfstate+tfcloud backend.\n", "Only used with tfstate+tfcloud backend.\n",
) )
fl.StringVar(&opts.BackendOptions.TFCloudEndpoint,
"tfc-endpoint",
"https://app.terraform.io/api/v2",
"Terraform Cloud / Enterprise API endpoint.\n"+
"Only used with tfstate+tfcloud backend.\n",
)
fl.String( fl.String(
"tf-provider-version", "tf-provider-version",
"", "",

View File

@ -20,8 +20,9 @@ var supportedBackends = []string{
type Backend io.ReadCloser type Backend io.ReadCloser
type Options struct { type Options struct {
Headers map[string]string Headers map[string]string
TFCloudToken string TFCloudToken string
TFCloudEndpoint string
} }
func IsSupported(backend string) bool { func IsSupported(backend string) bool {

View File

@ -13,11 +13,11 @@ import (
) )
type container struct { type container struct {
Credentials struct { Credentials map[string]containerToken
TerraformCloud struct { }
Token string
} `json:"app.terraform.io"` type containerToken struct {
} Token string
} }
type tfCloudConfigReader struct { type tfCloudConfigReader struct {
@ -28,7 +28,7 @@ func NewTFCloudConfigReader(reader io.ReadCloser) *tfCloudConfigReader {
return &tfCloudConfigReader{reader} return &tfCloudConfigReader{reader}
} }
func (r *tfCloudConfigReader) GetToken() (string, error) { func (r *tfCloudConfigReader) GetToken(host string) (string, error) {
b, err := ioutil.ReadAll(r.reader) b, err := ioutil.ReadAll(r.reader)
if err != nil { if err != nil {
return "", errors.New("unable to read file") return "", errors.New("unable to read file")
@ -38,10 +38,10 @@ func (r *tfCloudConfigReader) GetToken() (string, error) {
if err := json.Unmarshal(b, &container); err != nil { if err := json.Unmarshal(b, &container); err != nil {
return "", err return "", err
} }
if container.Credentials.TerraformCloud.Token == "" { if container.Credentials[host].Token == "" {
return "", errors.New("driftctl could not read your Terraform configuration file, please check that this is a valid Terraform credentials file") return "", errors.New("driftctl could not read your Terraform configuration file, please check that this is a valid Terraform credentials file")
} }
return container.Credentials.TerraformCloud.Token, nil return container.Credentials[host].Token, nil
} }
func getTerraformConfigFile() (string, error) { func getTerraformConfigFile() (string, error) {

View File

@ -44,7 +44,7 @@ func TestTFCloudConfigReader_GetToken(t *testing.T) {
readerCloser := ioutil.NopCloser(strings.NewReader(tt.src)) readerCloser := ioutil.NopCloser(strings.NewReader(tt.src))
defer readerCloser.Close() defer readerCloser.Close()
r := NewTFCloudConfigReader(readerCloser) r := NewTFCloudConfigReader(readerCloser)
got, err := r.GetToken() got, err := r.GetToken("app.terraform.io")
if err != nil && err.Error() != tt.wantErr.Error() { if err != nil && err.Error() != tt.wantErr.Error() {
t.Errorf("GetToken() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetToken() error = %v, wantErr %v", err, tt.wantErr)
return return

View File

@ -14,7 +14,6 @@ import (
) )
const BackendKeyTFCloud = "tfcloud" const BackendKeyTFCloud = "tfcloud"
const TFCloudAPI = "https://app.terraform.io/api/v2"
type TFCloudAttributes struct { type TFCloudAttributes struct {
HostedStateDownloadUrl string `json:"hosted-state-download-url"` HostedStateDownloadUrl string `json:"hosted-state-download-url"`
@ -36,7 +35,7 @@ type TFCloudBackend struct {
} }
func NewTFCloudReader(client pkghttp.HTTPClient, workspaceId string, opts *Options) (*TFCloudBackend, error) { func NewTFCloudReader(client pkghttp.HTTPClient, workspaceId string, opts *Options) (*TFCloudBackend, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/workspaces/%s/current-state-version", TFCloudAPI, workspaceId), nil) req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/workspaces/%s/current-state-version", opts.TFCloudEndpoint, workspaceId), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -57,7 +56,7 @@ func (t *TFCloudBackend) authorize() error {
} }
defer file.Close() defer file.Close()
reader := NewTFCloudConfigReader(file) reader := NewTFCloudConfigReader(file)
token, err = reader.GetToken() token, err = reader.GetToken(t.request.URL.Host)
if err != nil { if err != nil {
return err return err
} }

View File

@ -29,7 +29,8 @@ func TestTFCloudBackend_Read(t *testing.T) {
args: args{ args: args{
workspaceId: "workspaceId", workspaceId: "workspaceId",
options: &Options{ options: &Options{
TFCloudToken: "TOKEN", TFCloudToken: "TOKEN",
TFCloudEndpoint: "https://app.terraform.io/api/v2",
}, },
}, },
url: "https://app.terraform.io/api/v2/workspaces/workspaceId/current-state-version", url: "https://app.terraform.io/api/v2/workspaces/workspaceId/current-state-version",
@ -54,7 +55,8 @@ func TestTFCloudBackend_Read(t *testing.T) {
args: args{ args: args{
workspaceId: "wrong_workspaceId", workspaceId: "wrong_workspaceId",
options: &Options{ options: &Options{
TFCloudToken: "TOKEN", TFCloudToken: "TOKEN",
TFCloudEndpoint: "https://app.terraform.io/api/v2",
}, },
}, },
url: "https://app.terraform.io/api/v2/workspaces/wrong_workspaceId/current-state-version", url: "https://app.terraform.io/api/v2/workspaces/wrong_workspaceId/current-state-version",
@ -73,7 +75,8 @@ func TestTFCloudBackend_Read(t *testing.T) {
args: args{ args: args{
workspaceId: "workspaceId", workspaceId: "workspaceId",
options: &Options{ options: &Options{
TFCloudToken: "TOKEN", TFCloudToken: "TOKEN",
TFCloudEndpoint: "https://app.terraform.io/api/v2",
}, },
}, },
url: "https://app.terraform.io/api/v2/workspaces/workspaceId/current-state-version", url: "https://app.terraform.io/api/v2/workspaces/workspaceId/current-state-version",