Skip to content

Commit

Permalink
conditionally check agent id type
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaasman00 committed Nov 4, 2024
1 parent e5f9975 commit 649d5f4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
73 changes: 51 additions & 22 deletions updater/internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ type Storage struct {
Directory string `yaml:"directory"`
}

// translateManagerToSupervisor handles the bulk of converting from a v1 agent to a v2 agent directory
//
// 1. Find and read in manager.yaml
// 2. Read in agent_id, endpoint, and secret_key values from manager.yaml
// 3. Construct new supervisor config using values from manager.yaml
// 4. Create and write supervisor.yaml
// 5. Marshal previous agent_id into and create persistent_state.yaml file in supervisor_storage
func translateManagerToSupervisor(logger *zap.Logger, installDir string, hcePort int, rb rollback.Rollbacker) error {
// Open manager.yaml
managerPath := filepath.Clean(filepath.Join(installDir, "manager.yaml"))
Expand All @@ -301,20 +308,30 @@ func translateManagerToSupervisor(logger *zap.Logger, installDir string, hcePort
return fmt.Errorf("unmarshal manager yaml: %w", err)
}

err = createSupervisorConfig(logger, manager, installDir, hcePort, rb)
agentID, ok := manager["agent_id"].(string)
if !ok {
return errors.New("read in agent id")
}

var agentIDFormat = "UUID"
if agentIDIsULID([]byte(agentID)) {
agentIDFormat = "ULID"
}

err = createSupervisorConfig(logger, manager, installDir, agentIDFormat, hcePort, rb)
if err != nil {
return fmt.Errorf("create supervisor config: %w", err)
}

err = handleAgentIDConversion(logger, manager, installDir, rb)
err = handleAgentIDConversion(logger, agentID, installDir, rb)
if err != nil {
return fmt.Errorf("convert agent id: %w", err)
}

return nil
}

func createSupervisorConfig(logger *zap.Logger, manager map[string]any, installDir string, hcePort int, rb rollback.Rollbacker) error {
func createSupervisorConfig(logger *zap.Logger, manager map[string]any, installDir, agentIDFormat string, hcePort int, rb rollback.Rollbacker) error {
// Read manager values
var ok bool
var endpoint, secretKey string
Expand All @@ -331,7 +348,7 @@ func createSupervisorConfig(logger *zap.Logger, manager map[string]any, installD
Endpoint: endpoint,
Headers: Headers{
Authorization: "Secret-Key " + secretKey,
AgentIDFormat: "ULID",
AgentIDFormat: agentIDFormat,
},
TLS: TLS{
Insecure: true,
Expand Down Expand Up @@ -400,25 +417,30 @@ type PersistentState struct {
InstanceID string `yaml:"instance_id"`
}

func handleAgentIDConversion(logger *zap.Logger, manager map[string]any, installDir string, rb rollback.Rollbacker) error {
// Retrieve agent id and convert to UUID
agentID, ok := manager["agent_id"].(string)
if !ok {
return fmt.Errorf("read in agent id")
}
agentULID, err := ulid.Parse(agentID)
if err != nil {
return fmt.Errorf("parse agent id into a ULID: %w", err)
}
ulidBytes, err := agentULID.MarshalBinary()
if err != nil {
return fmt.Errorf("marshal ulid to bytes: %w", err)
}
agentUUID, err := uuid.FromBytes(ulidBytes)
if err != nil {
return fmt.Errorf("convert agent id to UUID: %w", err)
func handleAgentIDConversion(logger *zap.Logger, agentID string, installDir string, rb rollback.Rollbacker) error {
if agentIDIsULID([]byte(agentID)) {
agentULID, err := ulid.Parse(agentID)
if err != nil {
return fmt.Errorf("parse agent id into a ULID: %w", err)
}
ulidBytes, err := agentULID.MarshalBinary()
if err != nil {
return fmt.Errorf("marshal ulid to bytes: %w", err)
}
agentUUID, err := uuid.FromBytes(ulidBytes)
if err != nil {
return fmt.Errorf("convert agent id to UUID: %w", err)
}
agentID = agentUUID.String()
} else {
// verify agentID is a UUID
_, err := uuid.FromBytes([]byte(agentID))
if err != nil {
return fmt.Errorf("agent id is not a UUID: %w", err)
}
}
persistentStateYaml, err := yaml.Marshal(&PersistentState{InstanceID: agentUUID.String()})

persistentStateYaml, err := yaml.Marshal(&PersistentState{InstanceID: agentID})
if err != nil {
return fmt.Errorf("marshal persistent_state yaml: %w", err)
}
Expand Down Expand Up @@ -456,3 +478,10 @@ func handleAgentIDConversion(logger *zap.Logger, manager map[string]any, install
}
return nil
}

func agentIDIsULID(agentID []byte) bool {
if len(agentID) == 26 {
return true
}
return false
}
2 changes: 1 addition & 1 deletion updater/internal/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func TestInstallArtifacts(t *testing.T) {
})

err = installer.Install(rb)
require.ErrorContains(t, err, "failed to translate manager config into supervisor config: convert agent id: parse agent id into a ULID")
require.ErrorContains(t, err, "failed to translate manager config into supervisor config: convert agent id: agent id is not a UUID")

contentsEqual(t, outDirConfig, "# The original config file")
contentsEqual(t, outDirManager, "endpoint: localhost:3001\nsecret_key: secret\nagent_id: oo7")
Expand Down

0 comments on commit 649d5f4

Please sign in to comment.