Skip to content

Commit

Permalink
feat: add TLS skip for custom https minio endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>
  • Loading branch information
arbreezy committed Mar 30, 2024
1 parent 732fdd1 commit 5f5d82a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ _Adding a remote cache_
* AWS S3
* _As a prerequisite `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` are required as environmental variables._
* Configuration, ``` k8sgpt cache add s3 --region <aws region> --bucket <name> ```
* Minio Configuration ``` k8sgpt cache add s3 --bucket <name> --endpoint <http://localhost:9000> ```
* Minio Configuration with HTTP endpoint ``` k8sgpt cache add s3 --bucket <name> --endpoint <http://localhost:9000>```
* Minio Configuration with HTTPs endpoint, skipping TLS verification ``` k8sgpt cache add s3 --bucket <name> --endpoint <https://localhost:9000> --insecure```
* K8sGPT will create the bucket if it does not exist
* Azure Storage
* We support a number of [techniques](https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#2-authenticate-with-azure) to authenticate against Azure
Expand Down
4 changes: 3 additions & 1 deletion cmd/cache/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
containerName string
projectId string
endpoint string
insecure bool
)

// addCmd represents the add command
Expand All @@ -49,7 +50,7 @@ var addCmd = &cobra.Command{
}
fmt.Println(color.YellowString("Adding remote based cache"))
cacheType := args[0]
remoteCache, err := cache.NewCacheProvider(cacheType, bucketName, region, endpoint, storageAccount, containerName, projectId)
remoteCache, err := cache.NewCacheProvider(cacheType, bucketName, region, endpoint, storageAccount, containerName, projectId, insecure)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
Expand All @@ -66,6 +67,7 @@ func init() {
CacheCmd.AddCommand(addCmd)
addCmd.Flags().StringVarP(&region, "region", "r", "us-east-1", "The region to use for the AWS S3 or GCS cache")
addCmd.Flags().StringVarP(&endpoint, "endpoint", "e", "", "The S3 or minio endpoint")
addCmd.Flags().BoolVarP(&insecure, "insecure", "i", false, "Skip TLS verification for S3/Minio custom endpoint")
addCmd.Flags().StringVarP(&bucketName, "bucket", "b", "", "The name of the AWS S3 bucket to use for the cache")
addCmd.Flags().StringVarP(&projectId, "projectid", "p", "", "The GCP project ID")
addCmd.Flags().StringVarP(&storageAccount, "storageacc", "s", "", "The Azure storage account name of the container")
Expand Down
12 changes: 6 additions & 6 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ var ServeCmd = &cobra.Command{
envIsSet := backend != "" || password != "" || model != ""
if envIsSet {
aiProvider = &ai.AIProvider{
Name: backend,
Password: password,
Model: model,
BaseURL: baseURL,
Engine: engine,
Name: backend,
Password: password,
Model: model,
BaseURL: baseURL,
Engine: engine,
ProxyEndpoint: proxyEndpoint,
Temperature: temperature(),
Temperature: temperature(),
}

configAI.Providers = append(configAI.Providers, *aiProvider)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ai/azureopenai.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *AzureAIClient) Configure(config IAIConfig) error {
return azureModelMapping[model]

}

if proxyEndpoint != "" {
proxyUrl, err := url.Parse(proxyEndpoint)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *OpenAIClient) Configure(config IAIConfig) error {
Transport: transport,
}
}

client := openai.NewClientWithConfig(defaultConfig)
if client == nil {
return errors.New("error creating OpenAI client")
Expand Down
3 changes: 2 additions & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ParseCacheConfiguration() (CacheProvider, error) {
return cacheInfo, nil
}

func NewCacheProvider(cacheType, bucketname, region, endpoint, storageAccount, containerName, projectId string) (CacheProvider, error) {
func NewCacheProvider(cacheType, bucketname, region, endpoint, storageAccount, containerName, projectId string, insecure bool) (CacheProvider, error) {
cProvider := CacheProvider{}

switch {
Expand All @@ -62,6 +62,7 @@ func NewCacheProvider(cacheType, bucketname, region, endpoint, storageAccount, c
cProvider.S3.BucketName = bucketname
cProvider.S3.Region = region
cProvider.S3.Endpoint = endpoint
cProvider.S3.InsecureSkipVerify = insecure
default:
return CacheProvider{}, status.Error(codes.Internal, fmt.Sprintf("%s is not a valid option", cacheType))
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/cache/s3_based.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cache

import (
"bytes"
"crypto/tls"
"log"
"net/http"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -17,9 +19,10 @@ type S3Cache struct {
}

type S3CacheConfiguration struct {
Region string `mapstructure:"region" yaml:"region,omitempty"`
BucketName string `mapstructure:"bucketname" yaml:"bucketname,omitempty"`
Endpoint string `mapstructure:"endpoint" yaml:"endpoint,omitempty"`
Region string `mapstructure:"region" yaml:"region,omitempty"`
BucketName string `mapstructure:"bucketname" yaml:"bucketname,omitempty"`
Endpoint string `mapstructure:"endpoint" yaml:"endpoint,omitempty"`
InsecureSkipVerify bool `mapstructure:"insecure" yaml:"insecure,omitempty"`
}

func (s *S3Cache) Configure(cacheInfo CacheProvider) error {
Expand All @@ -37,6 +40,11 @@ func (s *S3Cache) Configure(cacheInfo CacheProvider) error {
if cacheInfo.S3.Endpoint != "" {
sess.Config.Endpoint = &cacheInfo.S3.Endpoint
sess.Config.S3ForcePathStyle = aws.Bool(true)
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: cacheInfo.S3.InsecureSkipVerify},
}
customClient := &http.Client{Transport: transport}
sess.Config.HTTPClient = customClient
}

s3Client := s3.New(sess)
Expand Down
17 changes: 14 additions & 3 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import (
"google.golang.org/grpc/status"
)

const (
notUsedBucket = ""
notUsedRegion = ""
notUsedEndpoint = ""
notUsedStorageAcc = ""
notUsedContainerName = ""
notUsedProjectId = ""
notUsedInsecure = false
)

func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (*schemav1.AddConfigResponse, error,
) {

Expand All @@ -23,11 +33,12 @@ func (h *handler) AddConfig(ctx context.Context, i *schemav1.AddConfigRequest) (

switch i.Cache.GetCacheType().(type) {
case *schemav1.Cache_AzureCache:
remoteCache, err = cache.NewCacheProvider("azure", "", "", "", i.Cache.GetAzureCache().StorageAccount, i.Cache.GetAzureCache().ContainerName, "")
remoteCache, err = cache.NewCacheProvider("azure", notUsedBucket, notUsedRegion, notUsedEndpoint, i.Cache.GetAzureCache().StorageAccount, i.Cache.GetAzureCache().ContainerName, notUsedProjectId, notUsedInsecure)

Check warning on line 36 in pkg/server/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/config.go#L36

Added line #L36 was not covered by tests
case *schemav1.Cache_S3Cache:
remoteCache, err = cache.NewCacheProvider("s3", i.Cache.GetS3Cache().BucketName, i.Cache.GetS3Cache().Region, i.Cache.GetS3Cache().Endpoint, "", "", "")
//TODO: (Aris) add the protobuf's Getters
remoteCache, err = cache.NewCacheProvider("s3", i.Cache.GetS3Cache().BucketName, i.Cache.GetS3Cache().Region, notUsedEndpoint, notUsedStorageAcc, notUsedContainerName, notUsedProjectId, notUsedInsecure)

Check warning on line 39 in pkg/server/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/config.go#L38-L39

Added lines #L38 - L39 were not covered by tests
case *schemav1.Cache_GcsCache:
remoteCache, err = cache.NewCacheProvider("gcs", i.Cache.GetGcsCache().BucketName, i.Cache.GetGcsCache().Region, "", "", "", i.Cache.GetGcsCache().GetProjectId())
remoteCache, err = cache.NewCacheProvider("gcs", i.Cache.GetGcsCache().BucketName, i.Cache.GetGcsCache().Region, notUsedEndpoint, notUsedStorageAcc, notUsedContainerName, i.Cache.GetGcsCache().GetProjectId(), notUsedInsecure)

Check warning on line 41 in pkg/server/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/config.go#L41

Added line #L41 was not covered by tests
default:
return resp, status.Error(codes.InvalidArgument, "Invalid cache configuration")
}
Expand Down

0 comments on commit 5f5d82a

Please sign in to comment.