898 consolidate and remove tfe addr [part 2] (#998)

Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
This commit is contained in:
Christian Mesh 2023-12-12 11:22:39 -05:00 committed by GitHub
parent 0430a4825a
commit 25a6f51732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 31 deletions

View File

@ -50,7 +50,7 @@ func TestRemote_config(t *testing.T) {
}{
"with_a_nonexisting_organization": {
config: cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("nonexisting"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -58,7 +58,7 @@ func TestRemote_config(t *testing.T) {
"prefix": cty.NullVal(cty.String),
}),
}),
confErr: "organization \"nonexisting\" at host app.terraform.io not found",
confErr: "organization \"nonexisting\" at host " + mockedBackendHost + " not found",
},
"with_a_missing_hostname": {
config: cty.ObjectVal(map[string]cty.Value{
@ -177,7 +177,7 @@ func TestRemote_versionConstraints(t *testing.T) {
}{
"compatible version": {
config: cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -189,7 +189,7 @@ func TestRemote_versionConstraints(t *testing.T) {
},
"version too old": {
config: cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -202,7 +202,7 @@ func TestRemote_versionConstraints(t *testing.T) {
},
"version too new": {
config: cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -743,7 +743,7 @@ func TestRemote_ServiceDiscoveryAliases(t *testing.T) {
b := New(testDisco(s))
diag := b.Configure(cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{

View File

@ -38,9 +38,9 @@ const (
)
var (
tfeHost = svchost.Hostname("app.terraform.io")
credsSrc = auth.StaticCredentialsSource(map[svchost.Hostname]map[string]interface{}{
tfeHost: {"token": testCred},
mockedBackendHost = "app.example.com"
credsSrc = auth.StaticCredentialsSource(map[svchost.Hostname]map[string]interface{}{
svchost.Hostname(mockedBackendHost): {"token": testCred},
})
)
@ -70,7 +70,7 @@ func testInput(t *testing.T, answers map[string]string) *mockInput {
func testBackendDefault(t *testing.T) (*Remote, func()) {
obj := cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -83,7 +83,7 @@ func testBackendDefault(t *testing.T) (*Remote, func()) {
func testBackendNoDefault(t *testing.T) (*Remote, func()) {
obj := cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("hashicorp"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -96,7 +96,7 @@ func testBackendNoDefault(t *testing.T) (*Remote, func()) {
func testBackendNoOperations(t *testing.T) (*Remote, func()) {
obj := cty.ObjectVal(map[string]cty.Value{
"hostname": cty.StringVal("app.terraform.io"),
"hostname": cty.StringVal(mockedBackendHost),
"organization": cty.StringVal("no-operations"),
"token": cty.NullVal(cty.String),
"workspaces": cty.ObjectVal(map[string]cty.Value{
@ -286,7 +286,7 @@ func testServer(t *testing.T) *httptest.Server {
return httptest.NewServer(mux)
}
// testDisco returns a *disco.Disco mapping app.terraform.io and
// testDisco returns a *disco.Disco mapping to mockedBackendHost and
// localhost to a local test server.
func testDisco(s *httptest.Server) *disco.Disco {
services := map[string]interface{}{
@ -297,7 +297,7 @@ func testDisco(s *httptest.Server) *disco.Disco {
d := disco.NewWithCredentialsSource(credsSrc)
d.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
d.ForceHostServices(svchost.Hostname("app.terraform.io"), services)
d.ForceHostServices(svchost.Hostname(mockedBackendHost), services)
d.ForceHostServices(svchost.Hostname("localhost"), services)
return d
}

View File

@ -61,7 +61,7 @@ var (
func skipIfTFENotEnabled(t *testing.T) {
if os.Getenv("TF_TFC_TEST") == "" {
t.Skip("this test accesses app.terraform.io; set TF_TFC_TEST=1 to run it")
t.Skip("this test accesses " + tfeHost + "; set TF_TFC_TEST=1 to run it")
}
}
@ -278,7 +278,7 @@ func testBackend(t *testing.T, obj cty.Value, handlers map[string]func(http.Resp
b.local = testLocalBackend(t, b)
b.input = true
baseURL, err := url.Parse("https://app.terraform.io")
baseURL, err := url.Parse("https://" + tfeHost)
if err != nil {
t.Fatalf("testBackend: failed to parse base URL for client")
}
@ -348,7 +348,7 @@ func testUnconfiguredBackend(t *testing.T) (*Cloud, func()) {
b.client.Variables = mc.Variables
b.client.Workspaces = mc.Workspaces
baseURL, err := url.Parse("https://app.terraform.io")
baseURL, err := url.Parse("https://" + tfeHost)
if err != nil {
t.Fatalf("testBackend: failed to parse base URL for client")
}

View File

@ -90,7 +90,7 @@ func (m *MockApplies) create(cvID, workspaceID string) (*tfe.Apply, error) {
}
id := GenerateID("apply-")
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
url := fmt.Sprintf("https://%s/_archivist/%s", tfeHost, id)
a := &tfe.Apply{
ID: id,
@ -205,7 +205,7 @@ func (m *MockConfigurationVersions) List(ctx context.Context, workspaceID string
func (m *MockConfigurationVersions) Create(ctx context.Context, workspaceID string, options tfe.ConfigurationVersionCreateOptions) (*tfe.ConfigurationVersion, error) {
id := GenerateID("cv-")
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
url := fmt.Sprintf("https://%s/_archivist/%s", tfeHost, id)
cv := &tfe.ConfigurationVersion{
ID: id,
@ -543,7 +543,7 @@ func newMockPlans(client *MockClient) *MockPlans {
// working directory to find the logfile.
func (m *MockPlans) create(cvID, workspaceID string) (*tfe.Plan, error) {
id := GenerateID("plan-")
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
url := fmt.Sprintf("https://%s/_archivist/%s", tfeHost, id)
p := &tfe.Plan{
ID: id,
@ -1372,7 +1372,7 @@ func (m *MockStateVersions) List(ctx context.Context, options *tfe.StateVersionL
func (m *MockStateVersions) Create(ctx context.Context, workspaceID string, options tfe.StateVersionCreateOptions) (*tfe.StateVersion, error) {
id := GenerateID("sv-")
runID := os.Getenv("TFE_RUN_ID")
url := fmt.Sprintf("https://app.terraform.io/_archivist/%s", id)
url := fmt.Sprintf("https://%s/_archivist/%s", tfeHost, id)
if runID != "" && (options.Run == nil || runID != options.Run.ID) {
return nil, fmt.Errorf("option.Run.ID does not contain the ID exported by TFE_RUN_ID")

View File

@ -35,6 +35,10 @@ import (
"golang.org/x/oauth2"
)
// This is HashiCorp's cloud host.
// There are a few special circumstances that depend on this whitelisted hostname.
const tfeHost = "app.terraform.io"
// LoginCommand is a Command implementation that runs an interactive login
// flow for a remote service host. It then stashes credentials in a tfrc
// file in the user's home directory.
@ -187,7 +191,7 @@ func (c *LoginCommand) Run(args []string) int {
case clientConfig.SupportedGrantTypes.Has(disco.OAuthAuthzCodeGrant):
// We prefer an OAuth code grant if the server supports it.
oauthToken, tokenDiags = c.interactiveGetTokenByCode(hostname, credsCtx, clientConfig)
case clientConfig.SupportedGrantTypes.Has(disco.OAuthOwnerPasswordGrant) && hostname == svchost.Hostname("app.terraform.io"):
case clientConfig.SupportedGrantTypes.Has(disco.OAuthOwnerPasswordGrant) && hostname == svchost.Hostname(tfeHost):
// The password grant type is allowed only for Terraform Cloud SaaS.
// Note this case is purely theoretical at this point, as TFC currently uses
// its own bespoke login protocol (tfe)
@ -227,7 +231,7 @@ func (c *LoginCommand) Run(args []string) int {
}
c.Ui.Output("\n---------------------------------------------------------------------------------\n")
if hostname == "app.terraform.io" { // Terraform Cloud
if hostname == tfeHost { // Terraform Cloud
var motd struct {
Message string `json:"msg"`
Errors []interface{} `json:"errors"`

View File

@ -74,7 +74,7 @@ func TestLogin(t *testing.T) {
"scopes": []interface{}{"app1.full_access", "app2.read_only"},
},
})
svcs.ForceHostServices(svchost.Hostname("app.terraform.io"), map[string]interface{}{
svcs.ForceHostServices(svchost.Hostname(tfeHost), map[string]interface{}{
// This represents Terraform Cloud, which does not yet support the
// login API, but does support its own bespoke tokens API.
"tfe.v2": ts.URL + "/api/v2",
@ -116,20 +116,20 @@ func TestLogin(t *testing.T) {
}
}))
t.Run("app.terraform.io (no login support)", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
t.Run(tfeHost+" (no login support)", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "yes" at the consent prompt, then paste a token with some
// accidental whitespace.
defer testInputMap(t, map[string]string{
"approve": "yes",
"token": " good-token ",
})()
status := c.Run([]string{"app.terraform.io"})
status := c.Run([]string{tfeHost})
if status != 0 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
}
credsSrc := c.Services.CredentialsSource()
creds, err := credsSrc.ForHost(svchost.Hostname("app.terraform.io"))
creds, err := credsSrc.ForHost(svchost.Hostname(tfeHost))
if err != nil {
t.Errorf("failed to retrieve credentials: %s", err)
}
@ -278,7 +278,7 @@ func TestLogin(t *testing.T) {
defer testInputMap(t, map[string]string{
"approve": "no",
})()
status := c.Run([]string{"app.terraform.io"})
status := c.Run([]string{tfeHost})
if status != 1 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
}
@ -293,7 +293,7 @@ func TestLogin(t *testing.T) {
defer testInputMap(t, map[string]string{
"approve": "y",
})()
status := c.Run([]string{"app.terraform.io"})
status := c.Run([]string{tfeHost})
if status != 1 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
}

View File

@ -53,7 +53,7 @@ func TestShowHuman(t *testing.T) {
Redacted: true,
Mode: plans.NormalMode,
Qualities: []plans.Quality{},
RunHeader: "[reset][yellow]To view this run in a browser, visit:\nhttps://app.terraform.io/app/example_org/example_workspace/runs/run-run-bugsBUGSbugsBUGS[reset]",
RunHeader: "[reset][yellow]To view this run in a browser, visit:\nhttps://app.example.com/app/example_org/example_workspace/runs/run-run-bugsBUGSbugsBUGS[reset]",
RunFooter: "[reset][green]Run status: planned and saved (confirmable)[reset]\n[green]Workspace is unlocked[reset]",
},
nil,
@ -139,7 +139,7 @@ func TestShowJSON(t *testing.T) {
Redacted: false,
Mode: plans.NormalMode,
Qualities: []plans.Quality{},
RunHeader: "[reset][yellow]To view this run in a browser, visit:\nhttps://app.terraform.io/app/example_org/example_workspace/runs/run-run-bugsBUGSbugsBUGS[reset]",
RunHeader: "[reset][yellow]To view this run in a browser, visit:\nhttps://app.example.com/app/example_org/example_workspace/runs/run-run-bugsBUGSbugsBUGS[reset]",
RunFooter: "[reset][green]Run status: planned and saved (confirmable)[reset]\n[green]Workspace is unlocked[reset]",
},
nil,