From 92f3583eba35e41c62b6cb696cf1edb6451a0bbd Mon Sep 17 00:00:00 2001 From: Pawan Pinjarkar Date: Mon, 7 Oct 2024 16:10:00 -0400 Subject: [PATCH] fix linting errors, unit tests, shellchecks --- .../agent/files/usr/local/bin/start-agent.sh | 2 +- .../usr/local/bin/start-cluster-installation.sh | 8 ++++---- .../usr/local/bin/wait-for-assisted-service.sh | 2 +- pkg/asset/agent/gencrypto/auth_utils.go | 1 - pkg/asset/agent/gencrypto/authconfig.go | 8 ++++---- pkg/asset/agent/gencrypto/authconfig_test.go | 16 +++++++++------- pkg/asset/agent/image/ignition_test.go | 17 +++++++++++------ 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/data/data/agent/files/usr/local/bin/start-agent.sh b/data/data/agent/files/usr/local/bin/start-agent.sh index 35715b8c375..92fdfbb03a0 100644 --- a/data/data/agent/files/usr/local/bin/start-agent.sh +++ b/data/data/agent/files/usr/local/bin/start-agent.sh @@ -8,7 +8,7 @@ INFRA_ENV_ID="" until [[ $INFRA_ENV_ID != "" && $INFRA_ENV_ID != "null" ]]; do sleep 5 >&2 echo "Querying assisted-service for infra-env-id..." - INFRA_ENV_ID=$(curl_assisted_service "/infra-envs" GET $USER_AUTH_TOKEN | jq -r '.[0].id') + INFRA_ENV_ID=$(curl_assisted_service "/infra-envs" GET "$USER_AUTH_TOKEN" | jq -r '.[0].id') done echo "Fetched infra-env-id and found: $INFRA_ENV_ID" diff --git a/data/data/agent/files/usr/local/bin/start-cluster-installation.sh b/data/data/agent/files/usr/local/bin/start-cluster-installation.sh index dfe56791d45..48ad90be0e6 100644 --- a/data/data/agent/files/usr/local/bin/start-cluster-installation.sh +++ b/data/data/agent/files/usr/local/bin/start-cluster-installation.sh @@ -10,7 +10,7 @@ cluster_id="" while [[ "${cluster_id}" = "" ]] do # Get cluster id - cluster_id=$(curl_assisted_service "/clusters" GET $USER_AUTH_TOKEN | jq -r .[].id) + cluster_id=$(curl_assisted_service "/clusters" GET "$USER_AUTH_TOKEN" | jq -r .[].id) if [[ "${cluster_id}" = "" ]]; then sleep 2 fi @@ -28,7 +28,7 @@ status_issue="90_start-install" num_known_hosts() { local known_hosts=0 local insufficient_hosts=0 - host_status=$(curl_assisted_service "/infra-envs/${INFRA_ENV_ID}/hosts" GET $USER_AUTH_TOKEN | jq -r .[].status) + host_status=$(curl_assisted_service "/infra-envs/${INFRA_ENV_ID}/hosts" GET "$USER_AUTH_TOKEN" | jq -r .[].status) if [[ -n ${host_status} ]]; then for status in ${host_status}; do if [[ "${status}" == "known" ]]; then @@ -58,7 +58,7 @@ clear_issue "${status_issue}" while [[ "${cluster_status}" != "installed" ]] do sleep 5 - cluster_info="$(curl_assisted_service "/clusters" GET $USER_AUTH_TOKEN)" + cluster_info="$(curl_assisted_service "/clusters" GET "$USER_AUTH_TOKEN")" cluster_status=$(printf '%s' "${cluster_info}" | jq -r .[].status) echo "Cluster status: ${cluster_status}" 1>&2 # Start the cluster install, if it transitions back to Ready due to a failure, @@ -66,7 +66,7 @@ do case "${cluster_status}" in "ready") echo "Starting cluster installation..." 1>&2 - res=$(curl_assisted_service "/clusters/${cluster_id}/actions/install" POST $USER_AUTH_TOKEN -w "%{http_code}" -o /dev/null) + res=$(curl_assisted_service "/clusters/${cluster_id}/actions/install" POST "$USER_AUTH_TOKEN" -w "%{http_code}" -o /dev/null) if [[ $res = "202" ]]; then printf '\nCluster installation started\n' 1>&2 fi diff --git a/data/data/agent/files/usr/local/bin/wait-for-assisted-service.sh b/data/data/agent/files/usr/local/bin/wait-for-assisted-service.sh index 711477adb26..bf9d65ce675 100644 --- a/data/data/agent/files/usr/local/bin/wait-for-assisted-service.sh +++ b/data/data/agent/files/usr/local/bin/wait-for-assisted-service.sh @@ -6,7 +6,7 @@ source "common.sh" echo "Waiting for assisted-service to be ready" -until curl_assisted_service "/infra-envs" GET $USER_AUTH_TOKEN -o /dev/null --silent --fail; do +until curl_assisted_service "/infra-envs" GET "$USER_AUTH_TOKEN" -o /dev/null --silent --fail; do printf '.' sleep 5 done diff --git a/pkg/asset/agent/gencrypto/auth_utils.go b/pkg/asset/agent/gencrypto/auth_utils.go index e28a19cf060..366c562c48b 100644 --- a/pkg/asset/agent/gencrypto/auth_utils.go +++ b/pkg/asset/agent/gencrypto/auth_utils.go @@ -29,7 +29,6 @@ func ParseToken(tokenString string) (jwt.MapClaims, error) { return claims, nil } - // ParseExpirationFromToken checks if the token is expired or not. func ParseExpirationFromToken(tokenString string) (time.Time, error) { claims, err := ParseToken(tokenString) diff --git a/pkg/asset/agent/gencrypto/authconfig.go b/pkg/asset/agent/gencrypto/authconfig.go index 5c5222c5d70..384e85d0c7c 100644 --- a/pkg/asset/agent/gencrypto/authconfig.go +++ b/pkg/asset/agent/gencrypto/authconfig.go @@ -35,9 +35,9 @@ var ( // AuthType holds the authenticator type for agent based installer. const ( - AuthType = "agent-installer-local" - agentPersona = "agentAuth" - userPersona = "userAuth" + AuthType = "agent-installer-local" + agentPersona = "agentAuth" + userPersona = "userAuth" watcherPersona = "watcherAuth" ) @@ -99,7 +99,7 @@ func (a *AuthConfig) Generate(_ context.Context, dependencies asset.Parents) err // Auth tokens expires after 48 hours expiry := time.Now().UTC().Add(48 * time.Hour) a.AuthTokenExpiry = expiry.Format(time.RFC3339) - + agentAuthToken, err := generateToken(agentPersona, privateKey, &expiry) if err != nil { return err diff --git a/pkg/asset/agent/gencrypto/authconfig_test.go b/pkg/asset/agent/gencrypto/authconfig_test.go index 7a0fe032df8..298f7f5c0b2 100644 --- a/pkg/asset/agent/gencrypto/authconfig_test.go +++ b/pkg/asset/agent/gencrypto/authconfig_test.go @@ -38,21 +38,23 @@ func TestAuthConfig_Generate(t *testing.T) { assert.NotEqual(t, authConfigAsset.UserAuthToken, authConfigAsset.WatcherAuthToken) // verify each token is signed with correct persona - claims , err := ParseToken(authConfigAsset.AgentAuthToken) + claims, err := ParseToken(authConfigAsset.AgentAuthToken) assert.NoError(t, err) - persona, _ := claims["sub"].(string) + persona, ok := claims["sub"].(string) assert.Equal(t, persona, agentPersona) + assert.Equal(t, ok, true) - claims , err = ParseToken(authConfigAsset.UserAuthToken) + claims, err = ParseToken(authConfigAsset.UserAuthToken) assert.NoError(t, err) - persona, _ = claims["sub"].(string) + persona, ok = claims["sub"].(string) assert.Equal(t, persona, userPersona) + assert.Equal(t, ok, true) - claims , err = ParseToken(authConfigAsset.WatcherAuthToken) + claims, err = ParseToken(authConfigAsset.WatcherAuthToken) assert.NoError(t, err) - persona, _ = claims["sub"].(string) + persona, ok = claims["sub"].(string) assert.Equal(t, persona, watcherPersona) - + assert.Equal(t, ok, true) }) } } diff --git a/pkg/asset/agent/image/ignition_test.go b/pkg/asset/agent/image/ignition_test.go index 3ae81d52355..36b6527219c 100644 --- a/pkg/asset/agent/image/ignition_test.go +++ b/pkg/asset/agent/image/ignition_test.go @@ -93,9 +93,11 @@ func TestIgnition_getTemplateData(t *testing.T) { clusterName := "test-agent-cluster-install.test" publicKey := "-----BEGIN EC PUBLIC KEY-----\nMHcCAQEEIOSCfDNmx0qe6dncV4tg==\n-----END EC PUBLIC KEY-----\n" - token := "someToken" + agentAuthToken := "agentAuthToken" + userAuthToken := "userAuthToken" + watcherAuthToken := "watcherAuthToken" rendezvousIP := "192.168.111.80" - templateData := getTemplateData(clusterName, pullSecret, releaseImageList, releaseImage, releaseImageMirror, publicContainerRegistries, "minimal-iso", infraEnvID, publicKey, gencrypto.AuthType, token, "", "", haveMirrorConfig, agentClusterInstall.Spec.ProvisionRequirements.ControlPlaneAgents, agentClusterInstall.Spec.ProvisionRequirements.WorkerAgents, osImage, proxy, rendezvousIP) + templateData := getTemplateData(clusterName, pullSecret, releaseImageList, releaseImage, releaseImageMirror, publicContainerRegistries, "minimal-iso", infraEnvID, publicKey, gencrypto.AuthType, agentAuthToken, userAuthToken, watcherAuthToken, "", "", haveMirrorConfig, agentClusterInstall.Spec.ProvisionRequirements.ControlPlaneAgents, agentClusterInstall.Spec.ProvisionRequirements.WorkerAgents, osImage, proxy, rendezvousIP) assert.Equal(t, clusterName, templateData.ClusterName) assert.Equal(t, "http", templateData.ServiceProtocol) assert.Equal(t, pullSecret, templateData.PullSecret) @@ -111,17 +113,20 @@ func TestIgnition_getTemplateData(t *testing.T) { assert.Equal(t, proxy, templateData.Proxy) assert.Equal(t, publicKey, templateData.PublicKeyPEM) assert.Equal(t, gencrypto.AuthType, templateData.AuthType) - assert.Equal(t, token, templateData.Token) + assert.Equal(t, agentAuthToken, templateData.AgentAuthToken) + assert.Equal(t, userAuthToken, templateData.UserAuthToken) + assert.Equal(t, watcherAuthToken, templateData.WatcherAuthToken) assert.Equal(t, rendezvousIP, templateData.RendezvousIP) } func TestIgnition_getRendezvousHostEnv(t *testing.T) { nodeZeroIP := "2001:db8::dead:beef" - token := "someToken" - rendezvousHostEnv := getRendezvousHostEnv("http", nodeZeroIP, token, workflow.AgentWorkflowTypeInstall) + agentAuthtoken := "agentAuthtoken" + userAuthToken := "userAuthToken" + rendezvousHostEnv := getRendezvousHostEnv("http", nodeZeroIP, agentAuthtoken, userAuthToken, workflow.AgentWorkflowTypeInstall) assert.Equal(t, - "NODE_ZERO_IP="+nodeZeroIP+"\nSERVICE_BASE_URL=http://["+nodeZeroIP+"]:8090/\nIMAGE_SERVICE_BASE_URL=http://["+nodeZeroIP+"]:8888/\nAGENT_AUTH_TOKEN="+token+"\nPULL_SECRET_TOKEN="+token+"\nWORKFLOW_TYPE=install\n", + "NODE_ZERO_IP="+nodeZeroIP+"\nSERVICE_BASE_URL=http://["+nodeZeroIP+"]:8090/\nIMAGE_SERVICE_BASE_URL=http://["+nodeZeroIP+"]:8888/\nPULL_SECRET_TOKEN="+agentAuthtoken+"\nUSER_AUTH_TOKEN="+userAuthToken+"\nWORKFLOW_TYPE=install\n", rendezvousHostEnv) }