Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Certs via Environement Variable #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions kubetest2/internal/awssdk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,49 @@ package awssdk

import (
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"os"

"k8s.io/klog"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"k8s.io/klog/v2"
)

// NewConfig returns an AWS SDK config
// It will panic if the cnfig cannot be created
func NewConfig() aws.Config {
c, err := config.LoadDefaultConfig(context.TODO())
c, err := config.LoadDefaultConfig(context.TODO(), withCertsEnv())
if err != nil {
klog.Fatalf("failed to create AWS SDK config: %v", err)
}
return c
}

func withCertsEnv() func(*config.LoadOptions) error {
return func(lo *config.LoadOptions) error {
certs := os.Getenv("CERTS_CONTENT")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit on naming:

Suggested change
certs := os.Getenv("CERTS_CONTENT")
certs := os.Getenv("ADDITIONAL_CA")

if certs != "" {
klog.Infof("Loading certificates from CERTS_CONTENT")
lo.HTTPClient = newHTTPClientWithCerts([]byte(certs))
} else {
klog.Warningf("CERTS_CONTENT environment variable is not set or empty")
}
return nil
}
}

func newHTTPClientWithCerts(certData []byte) *http.Client {
pool := x509.NewCertPool()
if ok := pool.AppendCertsFromPEM(certData); !ok {
klog.Fatalf("Failed to append provided certificates")
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be merging the additional CA's into the default pool instead of the pool only containing the one passed in env?

},
}
return &http.Client{Transport: transport}
}
Loading