Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Review context and names
Browse files Browse the repository at this point in the history
  • Loading branch information
pcantera committed Nov 21, 2022
1 parent 47587ea commit 082a956
Show file tree
Hide file tree
Showing 67 changed files with 660 additions and 480 deletions.
4 changes: 2 additions & 2 deletions api/api_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewHTTPClient(config *configuration.Config) (svc *HTTPClient, err error) {
// Loads CA Certificate
caCert, err := os.ReadFile(svc.config.Certificate.Ca)
if err != nil {
return nil, fmt.Errorf("cannot read IMCO CA cert: %v", err)
return nil, fmt.Errorf("cannot read "+configuration.CloudOrchestratorPlatformName+" CA cert: %v", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
Expand All @@ -63,7 +63,7 @@ func NewHTTPClient(config *configuration.Config) (svc *HTTPClient, err error) {
cert, err := tls.LoadX509KeyPair(svc.config.Certificate.Cert, svc.config.Certificate.Key)
if err != nil {
return nil, fmt.Errorf(
"cannot read IMCO API key (from '%s' and '%s'): %v",
"cannot read "+configuration.CloudOrchestratorPlatformName+" API key (from '%s' and '%s'): %v",
svc.config.Certificate.Cert,
svc.config.Certificate.Key,
err,
Expand Down
8 changes: 4 additions & 4 deletions api/api_http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func TestNewHTTPClient(t *testing.T) {
},
"if cannot read config API CA cert": {
config: InitConfig(),
expected: "cannot read IMCO CA cert",
expected: "cannot read " + configuration.CloudOrchestratorPlatformName + " CA cert",
},
"if cannot read config API key": {
config: InitConfig(),
expected: "cannot read IMCO API key",
expected: "cannot read " + configuration.CloudOrchestratorPlatformName + " API key",
},
"if config initialized": {
config: InitConfig(),
Expand All @@ -120,10 +120,10 @@ func TestNewHTTPClient(t *testing.T) {
test.config.Certificate.Key = ""
test.config.Certificate.Ca = ""
}
if test.expected == "cannot read IMCO CA cert" {
if test.expected == "cannot read "+configuration.CloudOrchestratorPlatformName+" CA cert" {
test.config.Certificate.Ca = testutils.TEST
}
if test.expected == "cannot read IMCO API key" {
if test.expected == "cannot read "+configuration.CloudOrchestratorPlatformName+" API key" {
test.config.Certificate.Key = testutils.TEST
}
}
Expand Down
79 changes: 39 additions & 40 deletions api/api_imco_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,37 @@ import (
"github.com/ingrammicro/cio/configuration"
"github.com/ingrammicro/cio/logger"
"github.com/ingrammicro/cio/types"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)

const (
pathBlueprintScriptCharacterizationsType = "/blueprint/script_characterizations?type=%s"
pathBlueprintScriptCharacterization = "/blueprint/script_characterizations/%s"
pathBlueprintScriptConclusions = "/blueprint/script_conclusions"
pathBlueprintConfiguration = "/blueprint/configuration"
pathBlueprintAppliedConfiguration = "/blueprint/applied_configuration"
pathBlueprintBootstrapLogs = "/blueprint/bootstrap_logs"
pathCloudFirewallProfile = "/cloud/firewall_profile"
pathCloudFirewallProfileRules = "/cloud/firewall_profile/rules"
pathCommandPollingPings = "/command_polling/pings"
pathCommandPollingNextCommand = "/command_polling/command"
pathCommandPollingCommand = "/command_polling/commands/%s"
pathCommandPollingBootstrapLogs = "/command_polling/bootstrap_logs"

pathBrownfieldSslProfile = "/brownfield/ssl_profile"
pathCommandPollingApiKey = "/command_polling/api_key"
pathBrownfieldSettings = "/brownfield/settings"
pathSecretVersionContent = "/secret/secret_versions/%s"
PathBlueprintScriptCharacterizationsType = "/blueprint/script_characterizations?type=%s"
PathBlueprintScriptCharacterization = "/blueprint/script_characterizations/%s"
PathBlueprintScriptConclusions = "/blueprint/script_conclusions"
PathBlueprintConfiguration = "/blueprint/configuration"
PathBlueprintAppliedConfiguration = "/blueprint/applied_configuration"
PathBlueprintBootstrapLogs = "/blueprint/bootstrap_logs"
PathCloudFirewallProfile = "/cloud/firewall_profile"
PathCloudFirewallProfileRules = "/cloud/firewall_profile/rules"
PathCommandPollingPings = "/command_polling/pings"
PathCommandPollingNextCommand = "/command_polling/command"
PathCommandPollingCommand = "/command_polling/commands/%s"
PathCommandPollingBootstrapLogs = "/command_polling/bootstrap_logs"

PathBrownfieldSslProfile = "/brownfield/ssl_profile"
PathCommandPollingApiKey = "/command_polling/api_key"
PathBrownfieldSettings = "/brownfield/settings"
PathSecretVersionContent = "/secret/secret_versions/%s"
)

// ServerAPI web service manager
type ServerAPI struct {
HTTPClient
}

// NewIMCOServerWithToken creates new http IMCO cli based on config
func NewIMCOServerWithToken(config *configuration.Config, context configuration.Context) (svc *ServerAPI, err error) {
// NewHTTPClientWithToken creates new http cli to orchestration platform based on config
func NewHTTPClientWithToken(config *configuration.Config, context configuration.Context) (svc *ServerAPI, err error) {
if config == nil {
return nil, fmt.Errorf(WebServiceConfigurationFailed)
}
Expand Down Expand Up @@ -80,7 +79,7 @@ func (imco *ServerAPI) GetDispatcherScriptCharacterizationsByType(ctx context.Co
logger.DebugFuncInfo()

_, err = imco.GetAndCheck(ctx,
fmt.Sprintf(pathBlueprintScriptCharacterizationsType, phase),
fmt.Sprintf(PathBlueprintScriptCharacterizationsType, phase),
true,
&scriptCharacterizations,
)
Expand All @@ -97,7 +96,7 @@ func (imco *ServerAPI) GetDispatcherScriptCharacterizationByUUID(ctx context.Con
logger.DebugFuncInfo()

_, err = imco.GetAndCheck(ctx,
fmt.Sprintf(pathBlueprintScriptCharacterization, scriptCharacterizationUUID),
fmt.Sprintf(PathBlueprintScriptCharacterization, scriptCharacterizationUUID),
true,
&scriptCharacterization,
)
Expand All @@ -112,7 +111,7 @@ func (imco *ServerAPI) ReportScriptConclusions(ctx context.Context, scriptConclu
) (command *types.ScriptConclusion, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.PostAndCheck(ctx, pathBlueprintScriptConclusions, scriptConclusions, true, &command)
status, err = imco.PostAndCheck(ctx, PathBlueprintScriptConclusions, scriptConclusions, true, &command)
if err != nil {
return nil, status, err
}
Expand All @@ -126,7 +125,7 @@ func (imco *ServerAPI) GetBootstrappingConfiguration(ctx context.Context) (
) {
logger.DebugFuncInfo()

status, err = imco.GetAndCheck(ctx, pathBlueprintConfiguration, true, &bootstrappingConfigurations)
status, err = imco.GetAndCheck(ctx, PathBlueprintConfiguration, true, &bootstrappingConfigurations)
if err != nil {
return nil, status, err
}
Expand All @@ -140,7 +139,7 @@ func (imco *ServerAPI) ReportBootstrappingAppliedConfiguration(ctx context.Conte
logger.DebugFuncInfo()

_, err = imco.PutAndCheck(ctx,
pathBlueprintAppliedConfiguration,
PathBlueprintAppliedConfiguration,
bootstrappingAppliedConfigurationParams,
true,
nil,
Expand All @@ -157,7 +156,7 @@ func (imco *ServerAPI) ReportBootstrappingLog(ctx context.Context,
) (command *types.BootstrappingContinuousReport, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.PostAndCheck(ctx, pathBlueprintBootstrapLogs, bootstrappingContinuousReportParams, false, &command)
status, err = imco.PostAndCheck(ctx, PathBlueprintBootstrapLogs, bootstrappingContinuousReportParams, false, &command)
if err != nil {
return nil, status, err
}
Expand All @@ -168,7 +167,7 @@ func (imco *ServerAPI) ReportBootstrappingLog(ctx context.Context,
func (imco *ServerAPI) GetPolicy(ctx context.Context) (policy *types.Policy, err error) {
logger.DebugFuncInfo()

_, err = imco.GetAndCheck(ctx, pathCloudFirewallProfile, true, &policy)
_, err = imco.GetAndCheck(ctx, PathCloudFirewallProfile, true, &policy)
if err != nil {
return nil, err
}
Expand All @@ -186,7 +185,7 @@ func (imco *ServerAPI) AddPolicyRule(ctx context.Context, ruleParams *map[string
) (policyRule *types.PolicyRule, err error) {
logger.DebugFuncInfo()

_, err = imco.PostAndCheck(ctx, pathCloudFirewallProfileRules, ruleParams, true, &policyRule)
_, err = imco.PostAndCheck(ctx, PathCloudFirewallProfileRules, ruleParams, true, &policyRule)
if err != nil {
return nil, err
}
Expand All @@ -198,7 +197,7 @@ func (imco *ServerAPI) UpdatePolicy(ctx context.Context, policyParams *map[strin
) (policy *types.Policy, err error) {
logger.DebugFuncInfo()

_, err = imco.PutAndCheck(ctx, pathCloudFirewallProfile, policyParams, true, &policy)
_, err = imco.PutAndCheck(ctx, PathCloudFirewallProfile, policyParams, true, &policy)
if err != nil {
return nil, err
}
Expand All @@ -210,7 +209,7 @@ func (imco *ServerAPI) Ping(ctx context.Context) (ping *types.PollingPing, statu
logger.DebugFuncInfo()

payload := make(map[string]interface{})
status, err = imco.PostAndCheck(ctx, pathCommandPollingPings, &payload, false, &ping)
status, err = imco.PostAndCheck(ctx, PathCommandPollingPings, &payload, false, &ping)
if err != nil {
return nil, status, err
}
Expand All @@ -221,7 +220,7 @@ func (imco *ServerAPI) Ping(ctx context.Context) (ping *types.PollingPing, statu
func (imco *ServerAPI) GetNextCommand(ctx context.Context) (command *types.PollingCommand, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.GetAndCheck(ctx, pathCommandPollingNextCommand, false, &command)
status, err = imco.GetAndCheck(ctx, PathCommandPollingNextCommand, false, &command)
if err != nil {
return nil, status, err
}
Expand All @@ -235,7 +234,7 @@ func (imco *ServerAPI) UpdateCommand(ctx context.Context, commandID string,
logger.DebugFuncInfo()

status, err = imco.PutAndCheck(ctx,
fmt.Sprintf(pathCommandPollingCommand, commandID),
fmt.Sprintf(PathCommandPollingCommand, commandID),
pollingCommandParams,
false,
&command,
Expand All @@ -252,7 +251,7 @@ func (imco *ServerAPI) ReportBootstrapLog(ctx context.Context, pollingContinuous
logger.DebugFuncInfo()

status, err = imco.PostAndCheck(ctx,
pathCommandPollingBootstrapLogs,
PathCommandPollingBootstrapLogs,
pollingContinuousReportParams,
false,
&command,
Expand All @@ -268,7 +267,7 @@ func (imco *ServerAPI) ObtainBrownfieldSslProfile(ctx context.Context, payload *
) (response map[string]interface{}, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.PostAndCheck(ctx, pathBrownfieldSslProfile, payload, false, &response)
status, err = imco.PostAndCheck(ctx, PathBrownfieldSslProfile, payload, false, &response)
if err != nil {
return nil, status, err
}
Expand All @@ -280,7 +279,7 @@ func (imco *ServerAPI) ObtainPollingApiKey(ctx context.Context, payload *map[str
) (response map[string]interface{}, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.PostAndCheck(ctx, pathCommandPollingApiKey, payload, false, &response)
status, err = imco.PostAndCheck(ctx, PathCommandPollingApiKey, payload, false, &response)
if err != nil {
return nil, status, err
}
Expand All @@ -292,7 +291,7 @@ func (imco *ServerAPI) SetFirewallProfile(ctx context.Context, policyParams *map
) (firewall *types.Firewall, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.PostAndCheck(ctx, pathCloudFirewallProfile, policyParams, false, &firewall)
status, err = imco.PostAndCheck(ctx, PathCloudFirewallProfile, policyParams, false, &firewall)
if err != nil {
return nil, status, err
}
Expand All @@ -303,7 +302,7 @@ func (imco *ServerAPI) SetFirewallProfile(ctx context.Context, policyParams *map
func (imco *ServerAPI) GetBrownfieldSettings(ctx context.Context) (settings *types.Settings, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.GetAndCheck(ctx, pathBrownfieldSettings, false, &settings)
status, err = imco.GetAndCheck(ctx, PathBrownfieldSettings, false, &settings)
if err != nil {
return nil, status, err
}
Expand All @@ -315,7 +314,7 @@ func (imco *ServerAPI) SetBrownfieldSettings(ctx context.Context, payload *map[s
) (settings *types.Settings, status int, err error) {
logger.DebugFuncInfo()

status, err = imco.PutAndCheck(ctx, pathBrownfieldSettings, payload, false, &settings)
status, err = imco.PutAndCheck(ctx, PathBrownfieldSettings, payload, false, &settings)
if err != nil {
return nil, status, err
}
Expand All @@ -324,10 +323,10 @@ func (imco *ServerAPI) SetBrownfieldSettings(ctx context.Context, payload *map[s

// RetrieveSecretVersion returns script characterizations list for a given UUID
func (imco *ServerAPI) RetrieveSecretVersion(ctx context.Context, svID, filePath string) (int, error) {
log.Debug("RetrieveSecretVersion")
logger.DebugFuncInfo()

_, status, err := imco.DownloadFile(ctx,
fmt.Sprintf("%s"+pathSecretVersionContent, imco.config.APIEndpoint, svID),
fmt.Sprintf("%s"+PathSecretVersionContent, imco.config.APIEndpoint, svID),
filePath,
false)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions api/api_imco_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func initConfigAndServerAPI(context configuration.Context) (*configuration.Confi
config.ServerID = testutils.TEST
}

svc, err := NewIMCOServerWithToken(config, context)
svc, err := NewHTTPClientWithToken(config, context)
if err != nil {
return nil, nil, err
}
return config, svc, nil
}

func TestNewIMCOServerWithToken(t *testing.T) {
func TestNewHTTPClientWithToken(t *testing.T) {
tests := map[string]struct {
context int
setConfig bool
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestNewIMCOServerWithToken(t *testing.T) {
test.config.ServerID = testutils.TEST
}

svc, err := NewIMCOServerWithToken(test.config, test.context)
svc, err := NewHTTPClientWithToken(test.config, test.context)
if err != nil && test.setConfig {
t.Errorf("Unexpected error: %v\n", err)
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package agent

import (
"context"
"fmt"
"github.com/ingrammicro/cio/logger"
"os"
Expand Down Expand Up @@ -56,7 +57,9 @@ func Register(context configuration.Context) {
f.PrintFatal("Must run as super-user", fmt.Errorf("running as non-administrator user"))
}
}
rootCACert, cert, key, err := obtainServerKeys(config, context)

ctx := cmd.GetContext()
rootCACert, cert, key, err := obtainServerKeys(ctx, config, context)
if err != nil {
f.PrintFatal("Couldn't obtain server keys", err)
}
Expand All @@ -67,10 +70,10 @@ func Register(context configuration.Context) {
fmt.Printf("CIO agent successfully registered, configuration file placed at %s\n", config.ConfFile)
}

func obtainServerKeys(config *configuration.Config, context configuration.Context) (
func obtainServerKeys(ctx context.Context, config *configuration.Config, context configuration.Context) (
rootCACert string, cert string, key string, err error,
) {
cs, err := api.NewIMCOServerWithToken(config, context)
cs, err := api.NewHTTPClientWithToken(config, context)
if err != nil {
return
}
Expand All @@ -80,10 +83,10 @@ func obtainServerKeys(config *configuration.Config, context configuration.Contex
responseData := make(map[string]interface{})

if context == configuration.Brownfield {
responseData, status, err = cs.ObtainBrownfieldSslProfile(cmd.GetContext(), &payload)
responseData, status, err = cs.ObtainBrownfieldSslProfile(ctx, &payload)
}
if context == configuration.Polling {
responseData, status, err = cs.ObtainPollingApiKey(cmd.GetContext(), &payload)
responseData, status, err = cs.ObtainPollingApiKey(ctx, &payload)
}
if err != nil {
return
Expand Down
9 changes: 3 additions & 6 deletions cmd/agent/bootstrapping/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,17 @@ const (
// defaultIntervalSeconds is the default minimum number of seconds
// we will wait between policyfile applications (or attempts)
defaultIntervalSeconds = 600 // 600 seconds = 10 minutes

// defaultSplaySeconds is the maximum number of seconds added to
// the configured interval to randomize the actual interval between
// policyfiles applications (or attempts)
defaultSplaySeconds = 300 // 300 seconds = 5 minutes

// defaultThresholdLines is the default maximum number of lines a
// batch of policyfile application output report can have
defaultThresholdLines = 20
// defaultApplyAfterIterations is the default number of iterations
// the continuous bootstrap command can go without attempting to apply
// policyfiles
defaultApplyAfterIterations = 4 // iterations

// ProcessLockFile is the name of the file used to ensure the bootstrap
// command is the only one of its kind running
ProcessLockFile = "cio-bootstrapping.lock"
Expand Down Expand Up @@ -196,7 +193,7 @@ func Stop() error {

formatter := format.GetFormatter()
if err := agent.StopProcess(lockFilePath()); err != nil {
formatter.PrintError("cannot Stop the bootstrapping process", err)
formatter.PrintError("cannot stop the bootstrapping process", err)
return err
}

Expand Down Expand Up @@ -408,7 +405,7 @@ func getBlueprintConfig(

// Inquire about desired configuration changes to be applied by querying the `GET /blueprint/configuration`
// endpoint. This will provide a JSON response with the desired configuration changes
blueprintConfig, status, err := svc.GetBootstrappingConfiguration(cmd.GetContext())
blueprintConfig, status, err := svc.GetBootstrappingConfiguration(ctx)
if err == nil && status != 200 {
err = fmt.Errorf("received non-ok %d response", status)
}
Expand Down Expand Up @@ -539,7 +536,7 @@ func downloadPolicyfiles(ctx context.Context, svc *api.ServerAPI, bsProcess *boo
for _, bsPolicyfile := range bsProcess.policyfiles {
tarballPath := bsPolicyfile.TarballPath(bsProcess.directoryPath)
log.Debug("downloading: ", tarballPath)
_, status, err := svc.DownloadFile(cmd.GetContext(), bsPolicyfile.DownloadURL, tarballPath, false)
_, status, err := svc.DownloadFile(ctx, bsPolicyfile.DownloadURL, tarballPath, false)
if err == nil && status != 200 {
err = fmt.Errorf("obtained non-ok response when downloading policyfile %s", bsPolicyfile.DownloadURL)
}
Expand Down
Loading

0 comments on commit 082a956

Please sign in to comment.