Skip to content

Commit 43463c8

Browse files
nupurg-ibmNupur Goyal
andauthored
fix: Fixing Bug related to the creation of service to service authorization, while using the existing bastion node (#176)
Co-authored-by: Nupur Goyal <nupurgoyal@Nupurs-MacBook-Pro.local>
1 parent e949783 commit 43463c8

File tree

17 files changed

+1163
-337
lines changed

17 files changed

+1163
-337
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ terraform.rc
5555

5656
# tweaks used locally
5757
localtweak__*.tf
58+
59+
# tests folder log file
60+
*.log

.tekton/scripts/ssh_create_delete.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CICD_SSH_KEY=$(echo $CICD_SSH_KEY-"$REVISION")
66
elif [ "${PR_REVISION}" ] && [ -z "${REVISION}" ]; then
77
CICD_SSH_KEY=$(echo $CICD_SSH_KEY-"$PR_REVISION")
88
else
9-
resource_group=$CICD_SSH_KEY-tekton
9+
CICD_SSH_KEY=$CICD_SSH_KEY-tekton
1010
fi
1111

1212
ssh_key_create() {
@@ -35,7 +35,7 @@ for region in "${REGIONS[@]}";
3535
check_key=$(eval "ibmcloud is keys | grep $CICD_SSH_KEY | awk '{print $2}'")
3636
if [[ -z "$check_key" ]]; then
3737
echo "$CICD_SSH_KEY creating in $region"
38-
ssh_key_create=$(eval "ibmcloud is key-create $CICD_SSH_KEY @/artifacts/.ssh/id_rsa.pub --resource-group-name $resource_group")
38+
ssh_key_create=$(eval "ibmcloud is key-create $CICD_SSH_KEY @/artifacts/.ssh/id_rsa.pub --resource-group-name ${resource_group:?}")
3939
if [[ $ssh_key_create = *Created* ]]; then
4040
echo "$CICD_SSH_KEY created in $region"
4141
else

.tekton/scripts/suites.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ ubuntu_suite_3() {
133133
# regions based suite on regions-suite
134134
regions_suite() {
135135
suite=regions-suite
136-
test_cases="TestRunInUsEastRegion,TestRunInEuDeRegion,TestRunInUSSouthRegion,TestRunCIDRsAsNonDefault,TestRunExistingPACEnvironment"
136+
test_cases="TestRunInUsEastRegion,TestRunInEuDeRegion,TestRunInUSSouthRegion,TestRunCIDRsAsNonDefault"
137137
new_line="${test_cases//,/$'\n'}"
138138
echo "************** Going to run ${suite} ${new_line} **************"
139139
common_suite "${test_cases}" "${suite}" "${compute_image_name_rhel:?}"

modules/landing_zone_vsi/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ module "login_vsi" {
123123
vpc_id = var.vpc_id
124124
kms_encryption_enabled = var.kms_encryption_enabled
125125
boot_volume_encryption_key = var.boot_volume_encryption_key
126-
skip_iam_authorization_policy = local.skip_iam_authorization_policy
126+
skip_iam_authorization_policy = var.bastion_instance_name != null ? false : local.skip_iam_authorization_policy
127+
existing_kms_instance_guid = var.existing_kms_instance_guid
127128
}
128129

129130
module "ldap_vsi" {

modules/landing_zone_vsi/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,9 @@ variable "ce_project_guid" {
409409
description = "The GUID of the Code Engine Project associated to this cluster Reservation"
410410
type = string
411411
}
412+
413+
variable "existing_kms_instance_guid" {
414+
type = string
415+
default = null
416+
description = "GUID of boot volume encryption key"
417+
}

solutions/hpc/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ module "landing_zone_vsi" {
128128
cloud_monitoring_prws_url = var.observability_monitoring_enable ? module.cloud_monitoring_instance_creation.cloud_monitoring_prws_url : ""
129129
bastion_instance_name = var.bastion_instance_name
130130
ce_project_guid = module.ce_project.guid
131+
existing_kms_instance_guid = local.existing_kms_instance_guid
131132
depends_on = [
132133
module.validate_ldap_server_connection
133134
]

tests/README.md

Lines changed: 295 additions & 60 deletions
Large diffs are not rendered by default.

tests/common_utils/deploy_utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Config struct {
5757
USSouthClusterID string `yaml:"us_south_cluster_id"`
5858
USSouthReservationID string `yaml:"us_south_reservation_id"`
5959
SSHFilePath string `yaml:"ssh_file_path"`
60+
SSHFilePathTwo string `yaml:"ssh_file_path_two"`
6061
}
6162

6263
// GetConfigFromYAML reads configuration from a YAML file and sets environment variables based on the configuration.
@@ -148,6 +149,7 @@ func setEnvFromConfig(config *Config) error {
148149
"US_SOUTH_RESERVATION_ID": config.USSouthReservationID,
149150
"US_SOUTH_CLUSTER_ID": config.USSouthClusterID,
150151
"SSH_FILE_PATH": config.SSHFilePath,
152+
"SSH_FILE_PATH_TWO": config.SSHFilePathTwo,
151153
}
152154

153155
for key, value := range envVars {

tests/common_utils/ssh_utils.go

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -167,73 +167,6 @@ func ConnectionE(t *testing.T, publicHostName, publicHostIP, privateHostName, pr
167167
return output, err
168168
}
169169

170-
// connectToHostsWithMultipleUsers establishes SSH connections to a host using multiple user credentials.
171-
// It takes the public and private IP addresses and host names for two different users.
172-
// Returns two SSH clients for the respective users, along with any errors encountered during the process.
173-
func ConnectToHostsWithMultipleUsers(publicHostName, publicHostIP, privateHostName, privateHostIP string) (*ssh.Client, *ssh.Client, error, error) {
174-
// Get the SSH private key file path for the first user from the environment variable
175-
sshKeyFilePathUserOne := os.Getenv("SSHFILEPATH")
176-
// Check if the file exists
177-
if _, err := os.Stat(sshKeyFilePathUserOne); os.IsNotExist(err) {
178-
return nil, nil, fmt.Errorf("SSH private key file '%s' does not exist", sshKeyFilePathUserOne), nil
179-
} else if err != nil {
180-
return nil, nil, fmt.Errorf("error checking SSH private key file: %v", err), nil
181-
}
182-
sshKeyUserOne, errUserOne := getSshKeyFile(sshKeyFilePathUserOne)
183-
if errUserOne != nil {
184-
return nil, nil, fmt.Errorf("failed to get SSH key for user one: %w", errUserOne), nil
185-
}
186-
187-
// Get the SSH private key file path for the second user from the environment variable
188-
sshKeyFilePathUserTwo := os.Getenv("SSHFILEPATHTWO")
189-
// Check if the file exists
190-
if _, err := os.Stat(sshKeyFilePathUserTwo); os.IsNotExist(err) {
191-
return nil, nil, nil, fmt.Errorf("SSH private key file '%s' does not exist", sshKeyFilePathUserTwo)
192-
} else if err != nil {
193-
return nil, nil, nil, fmt.Errorf("error checking SSH private key file: %v", err)
194-
}
195-
sshKeyUserTwo, errUserTwo := getSshKeyFile(sshKeyFilePathUserTwo)
196-
if errUserTwo != nil {
197-
return nil, nil, nil, fmt.Errorf("failed to get SSH key for user two: %w", errUserTwo)
198-
}
199-
200-
// Combine errors for better readability
201-
var combinedErrUserOne error
202-
if errUserOne != nil {
203-
combinedErrUserOne = fmt.Errorf("user one SSH key error: %v", errUserOne)
204-
}
205-
var combinedErrUserTwo error
206-
if errUserTwo != nil {
207-
combinedErrUserTwo = fmt.Errorf("user two SSH key error: %v", errUserTwo)
208-
}
209-
210-
if combinedErrUserOne != nil && combinedErrUserTwo != nil {
211-
return nil, nil, combinedErrUserOne, combinedErrUserTwo
212-
}
213-
214-
// Create SSH configurations for each user and host combination
215-
sshConfigUserOnePrivate := getSshConfig(sshKeyUserOne, privateHostName)
216-
sshConfigUserOnePublic := getSshConfig(sshKeyUserOne, publicHostName)
217-
sshConfigUserTwoPrivate := getSshConfig(sshKeyUserTwo, privateHostName)
218-
sshConfigUserTwoPublic := getSshConfig(sshKeyUserTwo, publicHostName)
219-
220-
// Establish SSH connections for each user to the host
221-
clientUserOne, errUserOne := sshClientJumpHost(sshConfigUserOnePrivate, sshConfigUserOnePublic, publicHostIP+":22", privateHostIP+":22")
222-
clientUserTwo, errUserTwo := sshClientJumpHost(sshConfigUserTwoPrivate, sshConfigUserTwoPublic, publicHostIP+":22", privateHostIP+":22")
223-
224-
// Combine errors for better readability
225-
var combinedErrClientUserOne error
226-
if errUserOne != nil {
227-
combinedErrClientUserOne = fmt.Errorf("user one unable to log in to the node: %v", errUserOne)
228-
}
229-
var combinedErrClientUserTwo error
230-
if errUserTwo != nil {
231-
combinedErrClientUserTwo = fmt.Errorf("user two unable to log in to the node: %v", errUserTwo)
232-
}
233-
234-
return clientUserOne, clientUserTwo, combinedErrClientUserOne, combinedErrClientUserTwo
235-
}
236-
237170
func ConnectToHostAsLDAPUser(publicHostName, publicHostIP, privateHostIP, ldapUser, ldapPassword string) (*ssh.Client, error) {
238171

239172
sshFilePath := os.Getenv("SSH_FILE_PATH")
@@ -267,3 +200,65 @@ func ConnectToHostAsLDAPUser(publicHostName, publicHostIP, privateHostIP, ldapUs
267200
}
268201
return sClient, nil
269202
}
203+
204+
// ConnectToHostsWithMultipleUsers establishes SSH connections to a host using multiple user credentials.
205+
// It takes the public and private IP addresses and host names for two different users.
206+
// Returns two SSH clients for the respective users, along with any errors encountered during the process.
207+
func ConnectToHostsWithMultipleUsers(publicHostName, publicHostIP, privateHostName, privateHostIP string) (*ssh.Client, *ssh.Client, error, error) {
208+
209+
// Get the SSH private key file path for the first user from the environment variable
210+
sshFilePath := os.Getenv("SSH_FILE_PATH")
211+
212+
// Check if the file exists
213+
_, err := os.Stat(sshFilePath)
214+
if os.IsNotExist(err) {
215+
return nil, nil, nil, fmt.Errorf("SSH private key file '%s' does not exist", sshFilePath)
216+
} else if err != nil {
217+
return nil, nil, nil, fmt.Errorf("error checking SSH private key file: %v", err)
218+
}
219+
220+
// Get the SSH key for the first user
221+
key, err := getSshKeyFile(sshFilePath)
222+
if err != nil {
223+
return nil, nil, nil, fmt.Errorf("failed to get SSH key: %w", err)
224+
}
225+
226+
// Create SSH configurations for the first user
227+
sshConfigUserOnePrivate := getSshConfig(key, publicHostName)
228+
sshConfigUserOnePublic := getSshConfig(key, privateHostName)
229+
230+
// Establish SSH connection for the first user
231+
clientUserOne, combinedErrClientUserOne := sshClientJumpHost(sshConfigUserOnePrivate, sshConfigUserOnePublic, publicHostIP+":22", privateHostIP+":22")
232+
if combinedErrClientUserOne != nil {
233+
return nil, nil, nil, fmt.Errorf("unable to log in to the node: %w", combinedErrClientUserOne)
234+
}
235+
236+
// Get the SSH private key file path for the second user from the environment variable
237+
sshFilePathTwo := os.Getenv("SSH_FILE_PATH_TWO")
238+
239+
// Check if the file exists
240+
_, err = os.Stat(sshFilePathTwo)
241+
if os.IsNotExist(err) {
242+
return nil, nil, nil, fmt.Errorf("SSH private key file '%s' does not exist", sshFilePathTwo)
243+
} else if err != nil {
244+
return nil, nil, nil, fmt.Errorf("error checking SSH private key file: %v", err)
245+
}
246+
247+
// Get the SSH key for the second user
248+
key2, err2 := getSshKeyFile(sshFilePathTwo)
249+
if err2 != nil {
250+
return nil, nil, nil, fmt.Errorf("failed to get SSH key: %w", err2)
251+
}
252+
253+
// Create SSH configurations for the second user
254+
sshConfigUserTwoPrivate := getSshConfig(key2, publicHostName)
255+
sshConfigUserTwoPublic := getSshConfig(key2, privateHostName)
256+
257+
// Establish SSH connection for the second user
258+
clientUserTwo, combinedErrClientUserTwo := sshClientJumpHost(sshConfigUserTwoPrivate, sshConfigUserTwoPublic, publicHostIP+":22", privateHostIP+":22")
259+
if combinedErrClientUserTwo != nil {
260+
return nil, nil, nil, fmt.Errorf("unable to log in to the node: %w", combinedErrClientUserTwo)
261+
}
262+
263+
return clientUserOne, clientUserTwo, combinedErrClientUserOne, combinedErrClientUserTwo
264+
}

0 commit comments

Comments
 (0)