Skip to content

Commit

Permalink
[CC-5718] Remove HCP token requirement during bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
jjacobson93 committed Jul 14, 2023
1 parent f4b0804 commit a8c6609
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
19 changes: 11 additions & 8 deletions agent/hcp/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,23 @@ func persistAndProcessConfig(dataDir string, devMode bool, bsCfg *hcpclient.Boot
return "", fmt.Errorf("failed to persist bootstrap config: %w", err)
}

if err := validateManagementToken(bsCfg.ManagementToken); err != nil {
return "", fmt.Errorf("invalid management token: %w", err)
}
if err := persistManagementToken(dir, bsCfg.ManagementToken); err != nil {
return "", fmt.Errorf("failed to persist HCP management token: %w", err)
if bsCfg.ManagementToken != "" {
if err := validateManagementToken(bsCfg.ManagementToken); err != nil {
return "", fmt.Errorf("invalid management token: %w", err)
}
if err := persistManagementToken(dir, bsCfg.ManagementToken); err != nil {
return "", fmt.Errorf("failed to persist HCP management token: %w", err)
}
}

if err := persistSucessMarker(dir); err != nil {
if err := persistSuccessMarker(dir); err != nil {
return "", fmt.Errorf("failed to persist success marker: %w", err)
}
}
return cfgJSON, nil
}

func persistSucessMarker(dir string) error {
func persistSuccessMarker(dir string) error {
name := filepath.Join(dir, successFileName)
return os.WriteFile(name, []byte(""), 0600)

Expand Down Expand Up @@ -454,7 +456,8 @@ func loadManagementToken(dir string) (string, error) {
name := filepath.Join(dir, tokenFileName)
bytes, err := os.ReadFile(name)
if os.IsNotExist(err) {
return "", errors.New("configuration files on disk are incomplete, missing: " + name)
// A missing management token is not an error, if none was provided by HCP
return "", nil
}
if err != nil {
return "", fmt.Errorf("failed to read: %w", err)
Expand Down
37 changes: 21 additions & 16 deletions agent/hcp/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,10 @@ func Test_loadPersistedBootstrapConfig(t *testing.T) {
warning string
}
type testCase struct {
existingCluster bool
mutateFn func(t *testing.T, dir string)
expect expect
existingCluster bool
disableManagementToken bool
mutateFn func(t *testing.T, dir string)
expect expect
}

run := func(t *testing.T, tc testCase) {
Expand All @@ -319,7 +320,7 @@ func Test_loadPersistedBootstrapConfig(t *testing.T) {

// Do some common setup as if we received config from HCP and persisted it to disk.
require.NoError(t, lib.EnsurePath(dir, true))
require.NoError(t, persistSucessMarker(dir))
require.NoError(t, persistSuccessMarker(dir))

if !tc.existingCluster {
caCert, caKey, err := tlsutil.GenerateCA(tlsutil.CAOpts{})
Expand All @@ -333,9 +334,12 @@ func Test_loadPersistedBootstrapConfig(t *testing.T) {
require.NoError(t, persistBootstrapConfig(dir, cfgJSON))
}

token, err := uuid.GenerateUUID()
require.NoError(t, err)
require.NoError(t, persistManagementToken(dir, token))
var token string
if !tc.disableManagementToken {
token, err = uuid.GenerateUUID()
require.NoError(t, err)
require.NoError(t, persistManagementToken(dir, token))
}

// Optionally mutate the persisted data to trigger errors while loading.
if tc.mutateFn != nil {
Expand All @@ -348,7 +352,6 @@ func Test_loadPersistedBootstrapConfig(t *testing.T) {
if loaded {
require.Equal(t, token, cfg.ManagementToken)
require.Empty(t, ui.ErrorWriter.String())

} else {
require.Nil(t, cfg)
require.Contains(t, ui.ErrorWriter.String(), tc.expect.warning)
Expand All @@ -365,15 +368,11 @@ func Test_loadPersistedBootstrapConfig(t *testing.T) {
warning: "",
},
},
"existing cluster missing token": {
existingCluster: true,
mutateFn: func(t *testing.T, dir string) {
// Remove the token file while leaving the existing cluster marker.
require.NoError(t, os.Remove(filepath.Join(dir, tokenFileName)))
},
"existing cluster no token": {
existingCluster: true,
disableManagementToken: true,
expect: expect{
loaded: false,
warning: "configuration files on disk are incomplete",
loaded: true,
},
},
"existing cluster no files": {
Expand All @@ -396,6 +395,12 @@ func Test_loadPersistedBootstrapConfig(t *testing.T) {
warning: "",
},
},
"new cluster with no token": {
disableManagementToken: true,
expect: expect{
loaded: true,
},
},
"new cluster some files": {
mutateFn: func(t *testing.T, dir string) {
// Remove one of the required files
Expand Down

0 comments on commit a8c6609

Please sign in to comment.