Skip to content

Commit d9fa439

Browse files
committed
fix(tests): Pull lab cert at run time for oauth tests
1 parent cd119fd commit d9fa439

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

.github/config/variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ variable "keyfactor_client_secret_12_3_0" {
2727
variable "keyfactor_hostname_12_3_0_KC" {
2828
description = "The hostname of the Keyfactor instance"
2929
type = string
30-
default = "int-oidc-lab.eastus2.cloudapp.azure.com"
30+
default = "int1230-oauth.eastus2.cloudapp.azure.com"
31+
3132
}
3233

3334
variable "keyfactor_auth_token_url_12_3_0_KC" {
3435
description = "The hostname of the KeyCloak instance to authenticate to for a Keyfactor Command access token"
3536
type = string
36-
default = "https://int-oidc-lab.eastus2.cloudapp.azure.com:8444/realms/Keyfactor/protocol/openid-connect/token"
37+
default = "https://int1230-oauth.eastus2.cloudapp.azure.com:8444/realms/Keyfactor/protocol/openid-connect/token"
3738
}
3839

auth_providers/auth_oauth_test.go

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package auth_providers_test
1616

1717
import (
18+
"crypto/tls"
19+
"encoding/pem"
1820
"fmt"
1921
"net/http"
22+
"net/url"
2023
"os"
24+
"path/filepath"
2125
"strings"
2226
"testing"
2327

@@ -107,7 +111,19 @@ func TestCommandConfigOauth_Authenticate(t *testing.T) {
107111
t.FailNow()
108112
}
109113

110-
caCertPath := "../lib/certs/int-oidc-lab.eastus2.cloudapp.azure.com.crt"
114+
hostName := os.Getenv(auth_providers.EnvKeyfactorHostName)
115+
caCertPath := fmt.Sprintf("../lib/certs/%s.crt", hostName)
116+
// check if the caCertPath exists and if not then reach out to host to get the cert and save it to the path
117+
if _, err := os.Stat(caCertPath); os.IsNotExist(err) {
118+
// get the cert from the host
119+
dErr := DownloadCertificate(hostName, caCertPath)
120+
if dErr != nil {
121+
t.Errorf("unable to download certificate from %s: %v", hostName, dErr)
122+
t.FailNow()
123+
}
124+
125+
// save the cert to the
126+
}
111127

112128
//Delete the config file
113129
t.Logf("Deleting config file: %s", configFilePath)
@@ -434,3 +450,83 @@ func unsetOAuthEnvVariables() {
434450
//os.Unsetenv(auth_providers.EnvKeyfactorDomain)
435451

436452
}
453+
454+
// DownloadCertificate fetches the SSL certificate chain from the given URL or hostname
455+
// while ignoring SSL verification and saves it to a file named "<hostname>.crt".
456+
func DownloadCertificate(input string, outputPath string) error {
457+
// Ensure the input has a scheme; default to "https://"
458+
if !strings.HasPrefix(input, "http://") && !strings.HasPrefix(input, "https://") {
459+
input = "https://" + input
460+
}
461+
462+
// Parse the URL
463+
parsedURL, err := url.Parse(input)
464+
if err != nil {
465+
return fmt.Errorf("invalid URL: %v", err)
466+
}
467+
468+
hostname := parsedURL.Hostname()
469+
if hostname == "" {
470+
return fmt.Errorf("could not determine hostname from URL: %s", input)
471+
}
472+
473+
// Set default output path to current working directory if none is provided
474+
if outputPath == "" {
475+
cwd, err := os.Getwd()
476+
if err != nil {
477+
return fmt.Errorf("failed to get current working directory: %v", err)
478+
}
479+
outputPath = cwd
480+
}
481+
482+
// Ensure the output directory exists
483+
if err := os.MkdirAll(outputPath, os.ModePerm); err != nil {
484+
return fmt.Errorf("failed to create output directory: %v", err)
485+
}
486+
487+
// Create the output file
488+
outputFile := filepath.Join(outputPath, fmt.Sprintf("%s.crt", hostname))
489+
file, err := os.Create(outputFile)
490+
if err != nil {
491+
return fmt.Errorf("failed to create file %s: %v", outputFile, err)
492+
}
493+
defer file.Close()
494+
495+
// Create an HTTP client that ignores SSL verification
496+
httpClient := &http.Client{
497+
Transport: &http.Transport{
498+
TLSClientConfig: &tls.Config{
499+
InsecureSkipVerify: true, // Ignore SSL certificate verification
500+
},
501+
},
502+
}
503+
504+
// Send an HTTP GET request to the server
505+
resp, err := httpClient.Get(input)
506+
if err != nil {
507+
return fmt.Errorf("failed to connect to %s: %v", input, err)
508+
}
509+
defer resp.Body.Close()
510+
511+
// Get the TLS connection state from the response
512+
tlsConnState := resp.TLS
513+
if tlsConnState == nil {
514+
return fmt.Errorf("no TLS connection state found")
515+
}
516+
517+
// Write the entire certificate chain to the output file in PEM format
518+
for _, cert := range tlsConnState.PeerCertificates {
519+
err = pem.Encode(
520+
file, &pem.Block{
521+
Type: "CERTIFICATE",
522+
Bytes: cert.Raw,
523+
},
524+
)
525+
if err != nil {
526+
return fmt.Errorf("failed to write certificate to file: %v", err)
527+
}
528+
}
529+
530+
fmt.Printf("Certificate chain saved to: %s\n", outputFile)
531+
return nil
532+
}

0 commit comments

Comments
 (0)