From c1ceff51858b027de8607083692f0862ed4603bb Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Tue, 13 Jan 2026 17:36:03 +0530 Subject: [PATCH 1/5] SK-2477 update vault creds at each layer --- v2/client/client.go | 208 +++++++++++------- v2/client/connection_service.go | 2 +- v2/client/vault_service.go | 6 +- v2/internal/helpers/helpers.go | 7 + v2/internal/validation/validations.go | 31 +++ .../vault/controller/connection_controller.go | 53 +++-- .../vault/controller/detect_controller.go | 49 +++-- .../vault/controller/vault_controller.go | 54 +++-- v2/utils/common/common.go | 1 - 9 files changed, 267 insertions(+), 144 deletions(-) diff --git a/v2/client/client.go b/v2/client/client.go index 9087ac04..3b26796e 100644 --- a/v2/client/client.go +++ b/v2/client/client.go @@ -2,13 +2,13 @@ package client import ( "fmt" + "github.com/skyflowapi/skyflow-go/v2/internal/validation" "github.com/skyflowapi/skyflow-go/v2/internal/vault/controller" vaultutils "github.com/skyflowapi/skyflow-go/v2/utils/common" error "github.com/skyflowapi/skyflow-go/v2/utils/error" "github.com/skyflowapi/skyflow-go/v2/utils/logger" logs "github.com/skyflowapi/skyflow-go/v2/utils/messages" - "os" ) type Skyflow struct { @@ -138,14 +138,14 @@ func (s *Skyflow) Vault(vaultID ...string) (*vaultService, *error.SkyflowError) return nil, err } - err = setVaultCredentials(config, s.credentials) - if err != nil { - return nil, err - } - err1 := validation.ValidateVaultConfig(*config) - if err1 != nil { - return nil, err1 - } + // err = setVaultCredentials(config, s.credentials) + // if err != nil { + // return nil, err + // } + // err1 := validation.ValidateVaultConfig(*config) + // if err1 != nil { + // return nil, err1 + // } vaultService := &vaultService{} // Get the VaultController from the builder's vaultServices map vaultService, exists := s.vaultServices[config.VaultId] @@ -155,8 +155,9 @@ func (s *Skyflow) Vault(vaultID ...string) (*vaultService, *error.SkyflowError) } // Update the config in the vaultapi service vaultService.controller = &controller.VaultController{ - Config: *config, - Loglevel: &s.logLevel, + Config: config, + Loglevel: &s.logLevel, + CommonCreds: s.credentials, } vaultService.config = config return vaultService, nil @@ -167,14 +168,14 @@ func (s *Skyflow) Connection(connectionId ...string) (*connectionService, *error if err != nil { return nil, err } - err = setConnectionCredentials(config, s.credentials) - if err != nil { - return nil, err - } - err1 := validation.ValidateConnectionConfig(*config) - if err1 != nil { - return nil, err1 - } + // err = setConnectionCredentials(config, s.credentials) + // if err != nil { + // return nil, err + // } + // err1 := validation.ValidateConnectionConfig(*config) + // if err1 != nil { + // return nil, err1 + // } connectionService := &connectionService{} connectionService, exists := s.connectionServices[config.ConnectionId] if !exists { @@ -182,9 +183,10 @@ func (s *Skyflow) Connection(connectionId ...string) (*connectionService, *error connectionService.logLevel = &s.logLevel s.connectionServices[config.ConnectionId] = connectionService } - connectionService.controller = controller.ConnectionController{ - Config: *config, + connectionService.controller = &controller.ConnectionController{ + Config: config, Loglevel: &s.logLevel, + CommonCreds: s.credentials, } connectionService.config = *config return connectionService, nil @@ -197,14 +199,14 @@ func (s *Skyflow) Detect(vaultID ...string) (*detectService, *error.SkyflowError return nil, err } - err = setDetectCredentials(config, s.credentials) - if err != nil { - return nil, err - } - err1 := validation.ValidateVaultConfig(*config) - if err1 != nil { - return nil, err1 - } + // err = setDetectCredentials(config, s.credentials) + // if err != nil { + // return nil, err + // } + // err1 := validation.ValidateVaultConfig(*config) + // if err1 != nil { + // return nil, err1 + // } detectService := &detectService{} // Get the VaultController from the builder's vaultServices map detectService, exists := s.detectServices[config.VaultId] @@ -214,8 +216,9 @@ func (s *Skyflow) Detect(vaultID ...string) (*detectService, *error.SkyflowError } // Update the config in the vaultapi service detectService.controller = &controller.DetectController{ - Config: *config, + Config: config, Loglevel: &s.logLevel, + CommonCreds: s.credentials, } detectService.config = config return detectService, nil @@ -237,6 +240,10 @@ func (s *Skyflow) GetConnection(connId string) (*vaultutils.ConnectionConfig, *e return &config.config, nil } +func (s *Skyflow) GetSkyflowCredentials() *vaultutils.Credentials { + return s.credentials +} + // UpdateLogLevel update methods func (s *Skyflow) UpdateLogLevel(logLevel logger.LogLevel) { logger.Info(fmt.Sprintf(logs.CURRENT_LOG_LEVEL, s.logLevel)) @@ -244,6 +251,9 @@ func (s *Skyflow) UpdateLogLevel(logLevel logger.LogLevel) { for _, service := range s.vaultServices { service.logLevel = &s.logLevel } + for _, service := range s.connectionServices { + service.logLevel = &s.logLevel + } for _, service := range s.detectServices { service.logLevel = &s.logLevel } @@ -256,27 +266,76 @@ func (s *Skyflow) UpdateSkyflowCredentials(credentials vaultutils.Credentials) * return err } s.credentials = &credentials + if (s.vaultServices != nil) && (len(s.vaultServices) > 0) { + for _, vService := range s.vaultServices { + if vService.controller != nil { + vService.controller.CommonCreds = &credentials + vService.controller.Token = "" + vService.controller.ApiKey = "" + } + } + } + if (s.detectServices != nil) && (len(s.detectServices) > 0) { + for _, dService := range s.detectServices { + if dService.controller != nil { + dService.controller.CommonCreds = &credentials + dService.controller.Token = "" + dService.controller.ApiKey = "" + } + } + } + if (s.connectionServices != nil) && (len(s.connectionServices) > 0) { + for _, cService := range s.connectionServices { + if cService.controller != nil { + cService.controller.CommonCreds = &credentials + cService.controller.Token = "" + cService.controller.ApiKey = "" + } + } + } return nil } func (s *Skyflow) UpdateVault(updatedConfig vaultutils.VaultConfig) *error.SkyflowError { logger.Info(logs.VALIDATING_VAULT_CONFIG) - e := validation.ValidateVaultConfig(updatedConfig) + e := validation.ValidateUpdateVaultConfig(updatedConfig) if e != nil { return e } if _, exists := s.vaultServices[updatedConfig.VaultId]; !exists { - logger.Error(fmt.Sprintf(logs.VAULT_CONFIG_DOES_NOT_EXIST, updatedConfig.VaultId)) - return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.VAULT_ID_NOT_IN_CONFIG_LIST) + if _, exists := s.detectServices[updatedConfig.VaultId]; !exists { + logger.Error(fmt.Sprintf(logs.VAULT_CONFIG_DOES_NOT_EXIST, updatedConfig.VaultId)) + return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.VAULT_ID_NOT_IN_CONFIG_LIST) + } } - s.vaultServices[updatedConfig.VaultId].config = &updatedConfig - - if _, exists := s.detectServices[updatedConfig.VaultId]; !exists { - logger.Error(fmt.Sprintf(logs.VAULT_CONFIG_DOES_NOT_EXIST, updatedConfig.VaultId)) - return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.VAULT_ID_NOT_IN_CONFIG_LIST) + // Update the credentials in the vaultapi controller if provided + if s.vaultServices[updatedConfig.VaultId].controller != nil { + if !isCredentialsEmpty(updatedConfig.Credentials) { + s.vaultServices[updatedConfig.VaultId].controller.Config.Credentials = updatedConfig.Credentials + s.vaultServices[updatedConfig.VaultId].controller.Token = "" + s.vaultServices[updatedConfig.VaultId].controller.ApiKey = "" + } + if updatedConfig.ClusterId != "" { + s.vaultServices[updatedConfig.VaultId].controller.Config.ClusterId = updatedConfig.ClusterId + } + s.vaultServices[updatedConfig.VaultId].controller.Config.Env = updatedConfig.Env + } + // Update the credentials in the detect controller if provided + if s.detectServices[updatedConfig.VaultId].controller != nil { + if !isCredentialsEmpty(updatedConfig.Credentials) { + s.detectServices[updatedConfig.VaultId].controller.Config.Credentials = updatedConfig.Credentials + s.detectServices[updatedConfig.VaultId].controller.Token = "" + s.detectServices[updatedConfig.VaultId].controller.ApiKey = "" + } + if updatedConfig.ClusterId != "" { + s.detectServices[updatedConfig.VaultId].controller.Config.ClusterId = updatedConfig.ClusterId + } + s.detectServices[updatedConfig.VaultId].controller.Config.Env = updatedConfig.Env } + // Update the config in the vaultapi service + s.vaultServices[updatedConfig.VaultId].config = &updatedConfig s.detectServices[updatedConfig.VaultId].config = &updatedConfig return nil } @@ -340,6 +399,35 @@ func (s *Skyflow) AddSkyflowCredentials(config vaultutils.Credentials) *error.Sk return err } s.credentials = &config + + if (s.vaultServices != nil) && (len(s.vaultServices) > 0) { + for _, vService := range s.vaultServices { + if vService.controller != nil { + vService.controller.CommonCreds = &config + vService.controller.Token = "" + vService.controller.ApiKey = "" + } + } + } + if (s.detectServices != nil) && (len(s.detectServices) > 0) { + for _, dService := range s.detectServices { + if dService.controller != nil { + dService.controller.CommonCreds = &config + dService.controller.Token = "" + dService.controller.ApiKey = "" + } + } + } + if (s.connectionServices != nil) && (len(s.connectionServices) > 0) { + for _, cService := range s.connectionServices { + if cService.controller != nil { + cService.controller.CommonCreds = &config + cService.controller.Token = "" + cService.controller.ApiKey = "" + } + } + } + return nil } @@ -429,55 +517,11 @@ func getDetectConfig(builder map[string]*detectService, vaultID ...string) (*vau return nil, error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.VAULT_ID_NOT_IN_CONFIG_LIST) } -func setVaultCredentials(config *vaultutils.VaultConfig, builderCreds *vaultutils.Credentials) *error.SkyflowError { - // here if credentials are empty in the vaultapi config - if config == nil || isCredentialsEmpty(config.Credentials) { - // here if builder credentials are available - if builderCreds != nil { - if !isCredentialsEmpty(*builderCreds) { - config.Credentials = *builderCreds - } - } else if envCreds := os.Getenv("SKYFLOW_CREDENTIALS"); envCreds != "" { - config.Credentials.CredentialsString = envCreds - } else { - return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.EMPTY_CREDENTIALS) - } - } - return nil -} func isCredentialsEmpty(creds vaultutils.Credentials) bool { return creds.Path == "" && creds.CredentialsString == "" && creds.Token == "" && creds.ApiKey == "" } -func setConnectionCredentials(config *vaultutils.ConnectionConfig, builderCreds *vaultutils.Credentials) *error.SkyflowError { - // here if credentials are empty in the vaultapi config - if config == nil || isCredentialsEmpty(config.Credentials) { - // here if builder credentials are available - if !isCredentialsEmpty(*builderCreds) { - config.Credentials = *builderCreds - } else if envCreds := os.Getenv("SKYFLOW_CREDENTIALS"); envCreds != "" { - config.Credentials.CredentialsString = envCreds - } else { - return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.EMPTY_CREDENTIALS) - } - } - return nil -} -func setDetectCredentials(config *vaultutils.VaultConfig, builderCreds *vaultutils.Credentials) *error.SkyflowError { - // here if credentials are empty in the vaultapi config - if config == nil || isCredentialsEmpty(config.Credentials) { - // here if builder credentials are available - if !isCredentialsEmpty(*builderCreds) { - config.Credentials = *builderCreds - } else if envCreds := os.Getenv("SKYFLOW_CREDENTIALS"); envCreds != "" { - config.Credentials.CredentialsString = envCreds - } else { - return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.EMPTY_CREDENTIALS) - } - } - return nil -} func getConnectionConfig(builder map[string]*connectionService, connectionId ...string) (*vaultutils.ConnectionConfig, *error.SkyflowError) { // if connection configs are empty if len(builder) == 0 { diff --git a/v2/client/connection_service.go b/v2/client/connection_service.go index f9413afe..fa678f8a 100644 --- a/v2/client/connection_service.go +++ b/v2/client/connection_service.go @@ -11,7 +11,7 @@ import ( type connectionService struct { config common.ConnectionConfig logLevel *logger.LogLevel - controller controller.ConnectionController + controller *controller.ConnectionController } func (c *connectionService) Invoke(ctx context.Context, request common.InvokeConnectionRequest) (*common.InvokeConnectionResponse, *skyflowError.SkyflowError) { diff --git a/v2/client/vault_service.go b/v2/client/vault_service.go index 00bc8c0c..c287c948 100644 --- a/v2/client/vault_service.go +++ b/v2/client/vault_service.go @@ -10,9 +10,9 @@ import ( ) type vaultService struct { - config *common.VaultConfig - logLevel *logger.LogLevel - controller *controller.VaultController + config *common.VaultConfig + logLevel *logger.LogLevel + controller *controller.VaultController } func (v *vaultService) Insert(ctx context.Context, request common.InsertRequest, options common.InsertOptions) (*common.InsertResponse, *skyflowError.SkyflowError) { diff --git a/v2/internal/helpers/helpers.go b/v2/internal/helpers/helpers.go index 8d4b20ff..a7a7791c 100644 --- a/v2/internal/helpers/helpers.go +++ b/v2/internal/helpers/helpers.go @@ -174,6 +174,13 @@ func GetFormattedQueryRecord(record vaultapis.V1FieldRecords) map[string]interfa for key, value := range record.Fields { queryRecord[key] = value } + if record.Tokens != nil && len(record.Tokens) > 0 { + tokens := make(map[string]interface{}) + for key, value := range record.Tokens { + tokens[key] = value + } + queryRecord["tokenized_data"] = tokens + } } return queryRecord } diff --git a/v2/internal/validation/validations.go b/v2/internal/validation/validations.go index 016e1f14..d8829b27 100644 --- a/v2/internal/validation/validations.go +++ b/v2/internal/validation/validations.go @@ -487,6 +487,18 @@ func ValidateVaultConfig(vaultConfig common.VaultConfig) *skyflowError.SkyflowEr } return nil } +func ValidateUpdateVaultConfig(vaultConfig common.VaultConfig) *skyflowError.SkyflowError { + // Validate VaultId + if vaultConfig.VaultId == "" { + logger.Error(logs.VAULT_ID_IS_REQUIRED) + return skyflowError.NewSkyflowError(skyflowError.INVALID_INPUT_CODE, skyflowError.INVALID_VAULT_ID) + } + // err := ValidateCredentials(vaultConfig.Credentials) + // if err != nil { + // return err + // } + return nil +} // ValidateConnectionConfig validates the ConnectionConfig struct func ValidateConnectionConfig(config common.ConnectionConfig) *skyflowError.SkyflowError { @@ -509,6 +521,25 @@ func ValidateConnectionConfig(config common.ConnectionConfig) *skyflowError.Skyf //} return nil } +func ValidateUpdateConnectionConfig(config common.ConnectionConfig) *skyflowError.SkyflowError { + if config.ConnectionId == "" { + logger.Error(logs.CONNECTION_ID_IS_REQUIRED) + return skyflowError.NewSkyflowError(skyflowError.INVALID_INPUT_CODE, skyflowError.EMPTY_CONNECTION_ID) + } + + if config.ConnectionUrl != "" { + _, err := url.Parse(config.ConnectionUrl) + if err != nil { + logger.Error(logs.INVALID_CONNECTION_URL) + return skyflowError.NewSkyflowError(skyflowError.INVALID_INPUT_CODE, skyflowError.INVALID_CONNECTION_URL) + } + } + // err := ValidateCredentials(config.Credentials) + // if err != nil { + // return err + // } + return nil +} // ValidateCredentials validates the credentials object func ValidateCredentials(credentials common.Credentials) *skyflowError.SkyflowError { diff --git a/v2/internal/vault/controller/connection_controller.go b/v2/internal/vault/controller/connection_controller.go index 064af49a..21135a8b 100644 --- a/v2/internal/vault/controller/connection_controller.go +++ b/v2/internal/vault/controller/connection_controller.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "mime/multipart" "net/http" + "os" "reflect" "strconv" "strings" @@ -16,7 +17,7 @@ import ( "github.com/skyflowapi/skyflow-go/v2/internal/validation" "github.com/skyflowapi/skyflow-go/v2/serviceaccount" "github.com/skyflowapi/skyflow-go/v2/utils/common" - "github.com/skyflowapi/skyflow-go/v2/utils/error" + errors "github.com/skyflowapi/skyflow-go/v2/utils/error" "github.com/skyflowapi/skyflow-go/v2/utils/logger" logs "github.com/skyflowapi/skyflow-go/v2/utils/messages" @@ -24,10 +25,11 @@ import ( ) type ConnectionController struct { - Config common.ConnectionConfig - Loglevel *logger.LogLevel - Token string - ApiKey string + Config *common.ConnectionConfig + CommonCreds *common.Credentials + Loglevel *logger.LogLevel + Token string + ApiKey string } var SetBearerTokenForConnectionControllerFunc = setBearerTokenForConnectionController @@ -36,32 +38,39 @@ var SetBearerTokenForConnectionControllerFunc = setBearerTokenForConnectionContr func setBearerTokenForConnectionController(v *ConnectionController) *errors.SkyflowError { // Validate token or generate a new one if expired or not set. // check if apikey or token already initalised - if v.ApiKey != "" { - logger.Info(logs.REUSE_API_KEY) - return nil - } else if v.Token != "" && serviceaccount.IsExpired(v.Token) { - logger.Info(logs.REUSE_BEARER_TOKEN) + credToUse, err := setConnectionCredentials(v.Config, v.CommonCreds) + if err != nil { + return err } - if v.Config.Credentials.ApiKey != "" { - v.ApiKey = v.Config.Credentials.ApiKey - } else if v.Config.Credentials.Token != "" { - if serviceaccount.IsExpired(v.Config.Credentials.Token) { - logger.Error(logs.BEARER_TOKEN_EXPIRED) - return errors.NewSkyflowError(errors.INVALID_INPUT_CODE, errors.TOKEN_EXPIRED) - } - v.Token = v.Config.Credentials.Token - return nil - } else if v.Token == "" || serviceaccount.IsExpired(v.Token) { + if v.Token == "" || serviceaccount.IsExpired(v.Token) { logger.Info(logs.GENERATE_BEARER_TOKEN_TRIGGERED) - token, err := GenerateToken(v.Config.Credentials) + token, err := GenerateToken(*credToUse) if err != nil { - logger.Error(logs.BEARER_TOKEN_REJECTED) return err } v.Token = *token + } else { + logger.Info(logs.REUSE_BEARER_TOKEN) } return nil } +func setConnectionCredentials(config *common.ConnectionConfig, builderCreds *common.Credentials) (*common.Credentials, *errors.SkyflowError) { + // here if credentials are empty in the vaultapi config + creds := common.Credentials{} + if config == nil || isCredentialsEmpty(config.Credentials) { + // here if builder credentials are available + if builderCreds != nil && !isCredentialsEmpty(*builderCreds) { + creds = *builderCreds + } else if envCreds := os.Getenv("SKYFLOW_CREDENTIALS"); envCreds != "" { + creds.CredentialsString = os.Getenv("SKYFLOW_CREDENTIALS") + } else { + return nil, errors.NewSkyflowError(errors.ErrorCodesEnum(errors.INVALID_INPUT_CODE), errors.EMPTY_CREDENTIALS) + } + } else { + creds = config.Credentials + } + return &creds, nil +} func (v *ConnectionController) Invoke(ctx context.Context, request common.InvokeConnectionRequest) (*common.InvokeConnectionResponse, *errors.SkyflowError) { tag := "Invoke Connection" diff --git a/v2/internal/vault/controller/detect_controller.go b/v2/internal/vault/controller/detect_controller.go index ed1ef37b..c61c3f9a 100644 --- a/v2/internal/vault/controller/detect_controller.go +++ b/v2/internal/vault/controller/detect_controller.go @@ -27,7 +27,8 @@ import ( ) type DetectController struct { - Config common.VaultConfig + Config *common.VaultConfig + CommonCreds *common.Credentials Loglevel *logger.LogLevel Token string ApiKey string @@ -89,10 +90,13 @@ func CreateDetectRequestClient(v *DetectController) *skyflowError.SkyflowError { // SetBearerTokenForDetectController checks and updates the token if necessary. func SetBearerTokenForDetectController(v *DetectController) *skyflowError.SkyflowError { - // Validate token or generate a new one if expired or not set. + credToUse, err := setVaultCredentials(v.Config, v.CommonCreds) + if err != nil { + return err + } if v.Token == "" || serviceaccount.IsExpired(v.Token) { logger.Info(logs.GENERATE_BEARER_TOKEN_TRIGGERED) - token, err := GenerateToken(v.Config.Credentials) + token, err := GenerateToken(*credToUse) if err != nil { return err } @@ -607,21 +611,21 @@ func (d *DetectController) DeidentifyText(ctx context.Context, request common.De if err := validation.ValidateDeidentifyTextRequest(request); err != nil { return nil, err } + // Ensure the bearer token is valid + if err := SetBearerTokenForDetectControllerFunc(d); err != nil { + logger.Error(logs.BEARER_TOKEN_REJECTED) + return nil, err + } // Create the API client if needed if err := CreateDetectRequestClientFunc(d); err != nil { - logger.Error(logs.BEARER_TOKEN_REJECTED, err) + logger.Error(logs.BEARER_TOKEN_REJECTED) return nil, err } - // Ensure the bearer token is valid - if err := SetBearerTokenForDetectControllerFunc(d); err != nil { - logger.Error(logs.BEARER_TOKEN_REJECTED, err) - return nil, err - } // Prepare the API request payload - apiRequest, err := CreateDeidentifyTextRequest(request, d.Config) + apiRequest, err := CreateDeidentifyTextRequest(request, *d.Config) if err != nil { return nil, err } @@ -701,19 +705,20 @@ func (d *DetectController) ReidentifyText(ctx context.Context, request common.Re return nil, err } - // Create the API client if needed - if err := CreateDetectRequestClientFunc(d); err != nil { + // Ensure the bearer token is valid + if err := SetBearerTokenForDetectControllerFunc(d); err != nil { + logger.Error(logs.BEARER_TOKEN_REJECTED) return nil, err } - // Ensure the bearer token is valid - if err := SetBearerTokenForDetectControllerFunc(d); err != nil { - logger.Error(logs.BEARER_TOKEN_REJECTED, err) + + // Create the API client if needed + if err := CreateDetectRequestClientFunc(d); err != nil { return nil, err } // Prepare the API request payload - apiRequest, err := CreateReidentifyTextRequest(request, d.Config) + apiRequest, err := CreateReidentifyTextRequest(request, *d.Config) if err != nil { return nil, err } @@ -748,14 +753,14 @@ func (d *DetectController) DeidentifyFile(ctx context.Context, request common.De return nil, err } - // Create the API client if needed - if err := CreateDetectRequestClientFunc(d); err != nil { + // Ensure the bearer token is valid + if err := SetBearerTokenForDetectControllerFunc(d); err != nil { + logger.Error(logs.BEARER_TOKEN_REJECTED) return nil, err } - // Ensure the bearer token is valid - if err := SetBearerTokenForDetectControllerFunc(d); err != nil { - logger.Error(logs.BEARER_TOKEN_REJECTED, err) + // Create the API client if needed + if err := CreateDetectRequestClientFunc(d); err != nil { return nil, err } @@ -1042,7 +1047,7 @@ func (d *DetectController) GetDetectRun(ctx context.Context, request common.GetD // Ensure the bearer token is valid if err := SetBearerTokenForDetectControllerFunc(d); err != nil { - logger.Error(logs.BEARER_TOKEN_REJECTED, err) + logger.Error(logs.BEARER_TOKEN_REJECTED) return nil, err } diff --git a/v2/internal/vault/controller/vault_controller.go b/v2/internal/vault/controller/vault_controller.go index 12918f1b..8b7c65d8 100644 --- a/v2/internal/vault/controller/vault_controller.go +++ b/v2/internal/vault/controller/vault_controller.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "os" constants "github.com/skyflowapi/skyflow-go/v2/internal/constants" vaultapis "github.com/skyflowapi/skyflow-go/v2/internal/generated" @@ -20,7 +21,8 @@ import ( ) type VaultController struct { - Config common.VaultConfig + Config *common.VaultConfig + CommonCreds *common.Credentials Loglevel *logger.LogLevel Token string ApiKey string @@ -40,13 +42,16 @@ func GenerateToken(credentials common.Credentials) (*string, *skyflowError.Skyfl options.Ctx = credentials.Context } switch { + case credentials.Token != "": + bearerToken = credentials.Token + case credentials.ApiKey != "": + bearerToken = credentials.ApiKey case credentials.Path != "": token, err := serviceaccount.GenerateBearerToken(credentials.Path, options) if err != nil { return nil, err } bearerToken = token.AccessToken - case credentials.CredentialsString != "": token, err := serviceaccount.GenerateBearerTokenFromCreds(credentials.CredentialsString, options) if err != nil { @@ -63,9 +68,13 @@ func GenerateToken(credentials common.Credentials) (*string, *skyflowError.Skyfl // SetBearerTokenForVaultController checks and updates the token if necessary. func SetBearerTokenForVaultController(v *VaultController) *skyflowError.SkyflowError { // Validate token or generate a new one if expired or not set. + credToUse, err := setVaultCredentials(v.Config, v.CommonCreds) + if err != nil { + return err + } if v.Token == "" || serviceaccount.IsExpired(v.Token) { logger.Info(logs.GENERATE_BEARER_TOKEN_TRIGGERED) - token, err := GenerateToken(v.Config.Credentials) + token, err := GenerateToken(*credToUse) if err != nil { return err } @@ -114,6 +123,29 @@ func CreateRequestClient(v *VaultController) *skyflowError.SkyflowError { v.ApiClient = *client return nil } +func setVaultCredentials(config *common.VaultConfig, builderCreds *common.Credentials) (*common.Credentials, *skyflowError.SkyflowError) { + // here if credentials are empty in the vaultapi config + creds := common.Credentials{} + if config == nil || isCredentialsEmpty(config.Credentials) { + // here if builder credentials are available + if builderCreds != nil && !isCredentialsEmpty(*builderCreds) { + creds = *builderCreds + } else if envCreds := os.Getenv("SKYFLOW_CREDENTIALS"); envCreds != "" { + creds.CredentialsString = os.Getenv("SKYFLOW_CREDENTIALS") + } else { + return nil, skyflowError.NewSkyflowError(skyflowError.ErrorCodesEnum(skyflowError.INVALID_INPUT_CODE), skyflowError.EMPTY_CREDENTIALS) + } + } else { + creds = config.Credentials + } + return &creds, nil +} + +func isCredentialsEmpty(creds common.Credentials) bool { + return creds.Path == "" && + creds.CredentialsString == "" && + creds.Token == "" && creds.ApiKey == "" +} func (v *VaultController) callBulkInsertAPI(ctx context.Context, body vaultapis.RecordServiceInsertRecordBody, table string) (*vaultapis.V1InsertRecordResponse, error) { bulkResp, err := v.ApiClient.Records.WithRawResponse.RecordServiceInsertRecord(ctx, v.Config.VaultId, table, &body) @@ -146,7 +178,6 @@ func (v *VaultController) Insert(ctx context.Context, request common.InsertReque // Initialize the response structure var resp common.InsertResponse var insertedFields, errors []map[string]interface{} - // Create the API client if err := CreateRequestClientFunc(v); err != nil { return nil, err @@ -359,7 +390,7 @@ func (v *VaultController) Delete(ctx context.Context, request common.DeleteReque if errs != nil { return nil, errs } - + if err := CreateRequestClientFunc(v); err != nil { return nil, err } @@ -390,8 +421,7 @@ func (v *VaultController) Query(ctx context.Context, queryRequest common.QueryRe return nil, errs } var fields []map[string]interface{} - var tokenizedData []map[string]interface{} - + if err := CreateRequestClientFunc(v); err != nil { return nil, err } @@ -407,10 +437,8 @@ func (v *VaultController) Query(ctx context.Context, queryRequest common.QueryRe if queryApiRes.Body != nil && queryApiRes.Body.GetRecords() != nil { for _, record := range queryApiRes.Body.GetRecords() { fields = append(fields, helpers.GetFormattedQueryRecord(*record)) - tokenizedData = append(tokenizedData, record.Tokens) } queryRes.Fields = fields - queryRes.TokenizedData = tokenizedData } logger.Info(logs.QUERY_REQUEST_RESOLVED) logger.Info(logs.QUERY_SUCCESS) @@ -425,7 +453,7 @@ func (v *VaultController) Update(ctx context.Context, request common.UpdateReque if errs != nil { return nil, errs } - + if err := CreateRequestClientFunc(v); err != nil { return nil, err } @@ -465,8 +493,8 @@ func (v *VaultController) Update(ctx context.Context, request common.UpdateReque updatedField = res updatedField["skyflowId"] = *id return &common.UpdateResponse{ - UpdatedField: updatedField, - Errors: nil, + UpdatedField: updatedField, + Errors: nil, }, nil } @@ -505,7 +533,7 @@ func (v *VaultController) UploadFile(ctx context.Context, request common.FileUpl if errs != nil { return nil, errs } - + if err := CreateRequestClientFunc(v); err != nil { return nil, err } diff --git a/v2/utils/common/common.go b/v2/utils/common/common.go index ad2da345..a1e794dd 100644 --- a/v2/utils/common/common.go +++ b/v2/utils/common/common.go @@ -496,6 +496,5 @@ type TokenizeResponse struct { } type QueryResponse struct { Fields []map[string]interface{} - TokenizedData []map[string]interface{} Errors []map[string]interface{} } From 4d85978e40435c1034f87ea6ae127c3cea6c96b2 Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Tue, 13 Jan 2026 18:33:05 +0530 Subject: [PATCH 2/5] SK-2477 updated tests --- v2/client/client_test.go | 392 +++++++++++++++++- .../vault/controller/controller_test.go | 48 +-- 2 files changed, 409 insertions(+), 31 deletions(-) diff --git a/v2/client/client_test.go b/v2/client/client_test.go index cf9eb49f..04d296e3 100644 --- a/v2/client/client_test.go +++ b/v2/client/client_test.go @@ -1,11 +1,12 @@ -package client_test +package client import ( - "testing" "fmt" + "os" + "testing" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - . "github.com/skyflowapi/skyflow-go/v2/client" "github.com/skyflowapi/skyflow-go/v2/utils/common" error "github.com/skyflowapi/skyflow-go/v2/utils/error" "github.com/skyflowapi/skyflow-go/v2/utils/logger" @@ -93,9 +94,9 @@ var _ = Describe("Skyflow Client", func() { It("should return error when initialize with custom invalid url", func() { var config []common.VaultConfig config = append(config, common.VaultConfig{ - VaultId: "id", - ClusterId: "cluster1", - Env: 0, + VaultId: "id", + ClusterId: "cluster1", + Env: 0, BaseVaultURL: "invalid-url", }) client, err := NewSkyflow( @@ -126,7 +127,7 @@ var _ = Describe("Skyflow Client", func() { customHeader := make(map[string]string) customHeader["x-custom-header"] = "custom-header-value" client, err := NewSkyflow( - WithVaults(config...), + WithVaults(config...), WithCustomHeaders(customHeader), ) Expect(client).ToNot(BeNil()) @@ -508,3 +509,380 @@ var _ = Describe("Detect and getDetectConfig scenarios", func() { Expect(service).NotTo(BeNil()) }) }) +var _ = Describe("Skyflow Management Methods", func() { + var client *Skyflow + var vaultConfig common.VaultConfig + var connConfig common.ConnectionConfig + var creds common.Credentials + + BeforeEach(func() { + vaultConfig = common.VaultConfig{ + VaultId: "vault1", + ClusterId: "cluster1", + Env: common.PROD, + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + connConfig = common.ConnectionConfig{ + ConnectionId: "conn1", + ConnectionUrl: "https://example.com", + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + creds = common.Credentials{ApiKey: os.Getenv("API_KEY")} + client, _ = NewSkyflow() + }) + + Context("AddVault", func() { + It("should add a vault successfully", func() { + err := client.AddVault(vaultConfig) + Expect(err).To(BeNil()) + Expect(client.vaultServices).To(HaveKey("vault1")) + }) + It("should not add duplicate vault", func() { + client.AddVault(vaultConfig) + err := client.AddVault(vaultConfig) + Expect(err).ToNot(BeNil()) + }) + }) + + Context("AddConnection", func() { + It("should add a connection successfully", func() { + err := client.AddConnection(connConfig) + Expect(err).To(BeNil()) + Expect(client.connectionServices).To(HaveKey("conn1")) + }) + It("should not add duplicate connection", func() { + client.AddConnection(connConfig) + err := client.AddConnection(connConfig) + Expect(err).ToNot(BeNil()) + }) + }) + + Context("AddSkyflowCredentials", func() { + It("should add credentials successfully", func() { + err := client.AddSkyflowCredentials(creds) + Expect(err).To(BeNil()) + Expect(client.credentials).To(Equal(&creds)) + }) + It("should fail with invalid credentials", func() { + invalidCreds := common.Credentials{} + err := client.AddSkyflowCredentials(invalidCreds) + Expect(err).ToNot(BeNil()) + }) + }) + + Context("GetVault", func() { + It("should get vault config", func() { + client.AddVault(vaultConfig) + cfg, err := client.GetVault("vault1") + Expect(err).To(BeNil()) + Expect(cfg.VaultId).To(Equal("vault1")) + }) + It("should fail for missing vault", func() { + cfg, err := client.GetVault("missing") + Expect(err).ToNot(BeNil()) + Expect(cfg).To(BeNil()) + }) + }) + + Context("GetConnection", func() { + It("should get connection config", func() { + client.AddConnection(connConfig) + cfg, err := client.GetConnection("conn1") + Expect(err).To(BeNil()) + Expect(cfg.ConnectionId).To(Equal("conn1")) + }) + It("should fail for missing connection", func() { + cfg, err := client.GetConnection("missing") + Expect(err).ToNot(BeNil()) + Expect(cfg).To(BeNil()) + }) + }) + + Context("UpdateLogLevel", func() { + It("should update log level", func() { + client.UpdateLogLevel(logger.DEBUG) + Expect(*client.GetLoglevel()).To(Equal(logger.DEBUG)) + }) + }) + + Context("UpdateSkyflowCredentials", func() { + It("should update credentials and propagate to controllers", func() { + client.AddVault(vaultConfig) + client.AddConnection(connConfig) + err := client.UpdateSkyflowCredentials(creds) + Expect(err).To(BeNil()) + Expect(client.credentials).To(Equal(&creds)) + // Check controllers updated + for _, v := range client.vaultServices { + if v.controller != nil { + Expect(v.controller.Config.Credentials).To(Equal(creds)) + } + } + }) + It("should fail with invalid credentials", func() { + invalidCreds := common.Credentials{} + err := client.UpdateSkyflowCredentials(invalidCreds) + Expect(err).ToNot(BeNil()) + }) + }) + + Context("UpdateVault", func() { + It("should update vault config and propagate to controller", func() { + client.AddVault(vaultConfig) + updated := vaultConfig + updated.ClusterId = "new-cluster" + err := client.UpdateVault(updated) + Expect(err).To(BeNil()) + Expect(client.vaultServices["vault1"].config.ClusterId).To(Equal("new-cluster")) + if client.vaultServices["vault1"].controller != nil { + Expect(client.vaultServices["vault1"].controller.Config.ClusterId).To(Equal("new-cluster")) + } + }) + It("should fail for missing vault", func() { + updated := vaultConfig + updated.VaultId = "missing" + err := client.UpdateVault(updated) + Expect(err).ToNot(BeNil()) + }) + }) + + Context("UpdateConnection", func() { + It("should update connection config", func() { + client.AddConnection(connConfig) + updated := connConfig + updated.ConnectionUrl = "https://new-url.com" + err := client.UpdateConnection(updated) + Expect(err).To(BeNil()) + Expect(client.connectionServices["conn1"].config.ConnectionUrl).To(Equal("https://new-url.com")) + }) + It("should fail for missing connection", func() { + updated := connConfig + updated.ConnectionId = "missing" + err := client.UpdateConnection(updated) + Expect(err).ToNot(BeNil()) + }) + }) + + Context("GetLoglevel", func() { + It("should get current log level", func() { + client.UpdateLogLevel(logger.INFO) + Expect(*client.GetLoglevel()).To(Equal(logger.INFO)) + }) + }) + + Context("RemoveVault", func() { + It("should remove vault", func() { + client.AddVault(vaultConfig) + err := client.RemoveVault("vault1") + Expect(err).To(BeNil()) + Expect(client.vaultServices).ToNot(HaveKey("vault1")) + }) + It("should fail for missing vault", func() { + err := client.RemoveVault("missing") + Expect(err).ToNot(BeNil()) + }) + }) + + Context("RemoveConnection", func() { + It("should remove connection", func() { + client.AddConnection(connConfig) + err := client.RemoveConnection("conn1") + Expect(err).To(BeNil()) + Expect(client.connectionServices).ToNot(HaveKey("conn1")) + }) + It("should fail for missing connection", func() { + err := client.RemoveConnection("missing") + Expect(err).ToNot(BeNil()) + }) + }) + Context("cross scenario: Vault(vaultid) and update/add/remove", func() { + var vaultConfig common.VaultConfig + var creds common.Credentials + BeforeEach(func() { + vaultConfig = common.VaultConfig{ + VaultId: "vaultX", + ClusterId: "clusterX", + Env: common.PROD, + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + creds = common.Credentials{ApiKey: os.Getenv("API_KEY")} + var err *error.SkyflowError + client, err = NewSkyflow(WithVaults(vaultConfig), WithCredentials(creds)) + Expect(err).To(BeNil()) + }) + It("should get vault by id, update, add, and remove", func() { + vaultSvc, err := client.Vault("vaultX") + Expect(err).To(BeNil()) + Expect(vaultSvc).ToNot(BeNil()) + Expect(vaultSvc.config.VaultId).To(Equal("vaultX")) + + updated := vaultConfig + updated.ClusterId = "new-clusterX" + err2 := client.UpdateVault(updated) + Expect(err2).To(BeNil()) + vault, err3 := client.GetVault("vaultX") + Expect(err3).To(BeNil()) + Expect(vault.ClusterId).To(Equal("new-clusterX")) + + vaultConfig2 := common.VaultConfig{ + VaultId: "vaultY", + ClusterId: "clusterY", + Env: common.PROD, + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + err4 := client.AddVault(vaultConfig2) + Expect(err4).To(BeNil()) + vault2, err5 := client.GetVault("vaultY") + Expect(err5).To(BeNil()) + Expect(vault2.VaultId).To(Equal("vaultY")) + + err6 := client.RemoveVault("vaultX") + Expect(err6).To(BeNil()) + _, err7 := client.GetVault("vaultX") + Expect(err7).ToNot(BeNil()) + }) + It("should fail to get, update, or remove missing vault", func() { + vaultSvc, err := client.Vault("missing") + Expect(err).ToNot(BeNil()) + Expect(vaultSvc).To(BeNil()) + missing := vaultConfig + missing.VaultId = "missing" + err2 := client.UpdateVault(missing) + Expect(err2).ToNot(BeNil()) + err3 := client.RemoveVault("missing") + Expect(err3).ToNot(BeNil()) + }) + }) + + Context("cross scenario: Detect(vaultid) and update/add/remove", func() { + var vaultConfig common.VaultConfig + var creds common.Credentials + BeforeEach(func() { + vaultConfig = common.VaultConfig{ + VaultId: "vaultDX", + ClusterId: "clusterDX", + Env: common.PROD, + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + creds = common.Credentials{ApiKey: os.Getenv("API_KEY")} + var err *error.SkyflowError + client, err = NewSkyflow(WithVaults(vaultConfig), WithCredentials(creds)) + Expect(err).To(BeNil()) + }) + It("should get detect by id, update vault, add, and remove", func() { + detectSvc, err := client.Detect("vaultDX") + Expect(err).To(BeNil()) + Expect(detectSvc).ToNot(BeNil()) + Expect(detectSvc.config.VaultId).To(Equal("vaultDX")) + + updated := vaultConfig + updated.ClusterId = "new-clusterDX" + err2 := client.UpdateVault(updated) + Expect(err2).To(BeNil()) + detect, err3 := client.Detect("vaultDX") + Expect(err3).To(BeNil()) + Expect(detect.config.ClusterId).To(Equal("new-clusterDX")) + + vaultConfig2 := common.VaultConfig{ + VaultId: "vaultDY", + ClusterId: "clusterDY", + Env: common.PROD, + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + err4 := client.AddVault(vaultConfig2) + Expect(err4).To(BeNil()) + detect2, err5 := client.Detect("vaultDY") + Expect(err5).To(BeNil()) + Expect(detect2.config.VaultId).To(Equal("vaultDY")) + + err6 := client.RemoveVault("vaultDX") + Expect(err6).To(BeNil()) + _, err7 := client.Detect("vaultDX") + Expect(err7).ToNot(BeNil()) + }) + It("should fail to get, update, or remove missing detect", func() { + detectSvc, err := client.Detect("missing") + Expect(err).ToNot(BeNil()) + Expect(detectSvc).To(BeNil()) + missing := vaultConfig + missing.VaultId = "missing" + err2 := client.UpdateVault(missing) + Expect(err2).ToNot(BeNil()) + err3 := client.RemoveVault("missing") + Expect(err3).ToNot(BeNil()) + }) + }) + + Context("cross scenario: Connection(connectionId) and update/add/remove", func() { + var connConfig common.ConnectionConfig + var creds common.Credentials + BeforeEach(func() { + connConfig = common.ConnectionConfig{ + ConnectionId: "connX", + ConnectionUrl: "https://connX.com", + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + creds = common.Credentials{ApiKey: os.Getenv("API_KEY")} + var err *error.SkyflowError + client, err = NewSkyflow(WithConnections(connConfig), WithCredentials(creds)) + Expect(err).To(BeNil()) + }) + It("should get connection by id, update, add, and remove", func() { + connSvc, err := client.Connection("connX") + Expect(err).To(BeNil()) + Expect(connSvc).ToNot(BeNil()) + Expect(connSvc.config.ConnectionId).To(Equal("connX")) + + updated := connConfig + updated.ConnectionUrl = "https://new-connX.com" + err2 := client.UpdateConnection(updated) + Expect(err2).To(BeNil()) + conn, err3 := client.GetConnection("connX") + Expect(err3).To(BeNil()) + Expect(conn.ConnectionUrl).To(Equal("https://new-connX.com")) + + connConfig2 := common.ConnectionConfig{ + ConnectionId: "connY", + ConnectionUrl: "https://connY.com", + Credentials: common.Credentials{ + ApiKey: os.Getenv("API_KEY"), + }, + } + err4 := client.AddConnection(connConfig2) + Expect(err4).To(BeNil()) + conn2, err5 := client.GetConnection("connY") + Expect(err5).To(BeNil()) + Expect(conn2.ConnectionId).To(Equal("connY")) + + err6 := client.RemoveConnection("connX") + Expect(err6).To(BeNil()) + _, err7 := client.GetConnection("connX") + Expect(err7).ToNot(BeNil()) + }) + It("should fail to get, update, or remove missing connection", func() { + connSvc, err := client.Connection("missing") + Expect(err).ToNot(BeNil()) + Expect(connSvc).To(BeNil()) + missing := connConfig + missing.ConnectionId = "missing" + err2 := client.UpdateConnection(missing) + Expect(err2).ToNot(BeNil()) + err3 := client.RemoveConnection("missing") + Expect(err3).ToNot(BeNil()) + }) + }) +}) diff --git a/v2/internal/vault/controller/controller_test.go b/v2/internal/vault/controller/controller_test.go index 71024f3b..03a810a0 100644 --- a/v2/internal/vault/controller/controller_test.go +++ b/v2/internal/vault/controller/controller_test.go @@ -70,7 +70,7 @@ var _ = Describe("Vault controller Test cases", func() { response = make(map[string]interface{}) ts = nil contrl = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "id", ClusterId: "clusterid", Env: PROD, @@ -176,7 +176,7 @@ var _ = Describe("Vault controller Test cases", func() { // Create the VaultController instance contrl := VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "id", ClusterId: "clusterid", Env: PROD, @@ -210,7 +210,7 @@ var _ = Describe("Vault controller Test cases", func() { // Create the VaultController instance contrl := VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "id", ClusterId: "clusterid", Env: PROD, @@ -270,7 +270,7 @@ var _ = Describe("Vault controller Test cases", func() { // Create the VaultController instance contrl := VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "id", ClusterId: "clusterid", Env: PROD, @@ -379,7 +379,7 @@ var _ = Describe("Vault controller Test cases", func() { // Create the VaultController instance contrl := VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "id", ClusterId: "clusterid", Env: PROD, @@ -451,7 +451,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = &VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -542,7 +542,7 @@ var _ = Describe("Vault controller Test cases", func() { }) It("should return detokenized data with partial success response", func() { _ = &VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{Token: "token"}, Env: PROD, @@ -603,7 +603,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -713,7 +713,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -797,7 +797,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -881,7 +881,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -971,7 +971,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -1056,7 +1056,7 @@ var _ = Describe("Vault controller Test cases", func() { BeforeEach(func() { // Initialize the VaultController instance vaultController = VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vaultID", Credentials: Credentials{ ApiKey: "sky-token", @@ -1157,7 +1157,7 @@ var _ = Describe("ConnectionController", func() { BeforeEach(func() { mockToken = "mock-valid-token" ctrl = &ConnectionController{ - Config: ConnectionConfig{ + Config: &ConnectionConfig{ ConnectionUrl: "http://mockserver.com", ConnectionId: "demo", }, @@ -1440,7 +1440,7 @@ var _ = Describe("VaultController", func() { BeforeEach(func() { vaultController = &VaultController{ - Config: VaultConfig{ + Config: &VaultConfig{ Credentials: Credentials{ Path: "test/path", }, @@ -1456,7 +1456,7 @@ var _ = Describe("VaultController", func() { vaultController.Config.Credentials.Context = "demo" err := SetBearerTokenForVaultController(vaultController) - Expect(err).ToNot(BeNil()) + Expect(err).To(BeNil()) }) It("should create token if the current token is expired", func() { vaultController.Config.Credentials.Token = os.Getenv("EXPIRED_TOKEN") @@ -1482,7 +1482,7 @@ var _ = Describe("VaultController", func() { Expect(err).To(BeNil()) Expect(vaultController.Token).ToNot(BeNil()) - vaultController.Config.Credentials.Path = "" + // vaultController.Config.Credentials.Path = "" errs := SetBearerTokenForVaultController(vaultController) Expect(errs).To(BeNil()) Expect(vaultController.Token).ToNot(BeNil()) @@ -1584,7 +1584,7 @@ var _ = Describe("DetectController", func() { BeforeEach(func() { detectController = &DetectController{ - Config: VaultConfig{ + Config: &VaultConfig{ Credentials: Credentials{ Path: "credentials.json", }, @@ -1601,7 +1601,7 @@ var _ = Describe("DetectController", func() { detectController.Config.Credentials.Context = "demo" err := SetBearerTokenForDetectControllerFunc(detectController) - Expect(err).ToNot(BeNil()) + Expect(err).To(BeNil()) }) It("should create token if the current token is expired", func() { detectController.Config.Credentials.Token = os.Getenv("EXPIRED_TOKEN") @@ -1626,7 +1626,7 @@ var _ = Describe("DetectController", func() { Expect(err).To(BeNil()) Expect(detectController.Token).ToNot(BeNil()) - detectController.Config.Credentials.Path = "" + // detectController.Config.Credentials.Path = "" errs := SetBearerTokenForDetectControllerFunc(detectController) Expect(errs).To(BeNil()) Expect(detectController.Token).ToNot(BeNil()) @@ -1969,7 +1969,7 @@ var _ = Describe("DetectController", func() { BeforeEach(func() { ctx = context.Background() detectController = &DetectController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vault123", ClusterId: "cluster123", Env: DEV, @@ -2238,7 +2238,7 @@ var _ = Describe("DetectController", func() { BeforeEach(func() { ctx = context.Background() detectController = &DetectController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vault123", ClusterId: "cluster123", Env: DEV, @@ -2404,7 +2404,7 @@ var _ = Describe("DetectController", func() { BeforeEach(func() { ctx = context.Background() detectController = &DetectController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vault123", ClusterId: "cluster123", Env: DEV, @@ -2944,7 +2944,7 @@ var _ = Describe("DetectController", func() { BeforeEach(func() { ctx = context.Background() detectController = &DetectController{ - Config: VaultConfig{ + Config: &VaultConfig{ VaultId: "vault123", ClusterId: "cluster123", Env: DEV, From f143837f8a0c4bef27469050d09a9f2af0738a0c Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Tue, 13 Jan 2026 19:50:18 +0530 Subject: [PATCH 3/5] SK-2477 updated tests --- v2/client/client.go | 29 ++++++++++++++++++++--------- v2/client/connection_service.go | 2 +- v2/internal/constants/constants.go | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/v2/client/client.go b/v2/client/client.go index 57d51d37..9b319ba6 100644 --- a/v2/client/client.go +++ b/v2/client/client.go @@ -102,7 +102,7 @@ func WithConnections(config ...vaultutils.ConnectionConfig) Option { // create the connection service s.connectionServices[connectionConfig.ConnectionId] = &connectionService{ - config: connectionConfig, + config: &connectionConfig, logLevel: &s.logLevel, } } @@ -188,7 +188,7 @@ func (s *Skyflow) Connection(connectionId ...string) (*connectionService, *error connectionService := &connectionService{} connectionService, exists := s.connectionServices[config.ConnectionId] if !exists { - connectionService.config = *config + connectionService.config = config connectionService.logLevel = &s.logLevel s.connectionServices[config.ConnectionId] = connectionService } @@ -197,7 +197,7 @@ func (s *Skyflow) Connection(connectionId ...string) (*connectionService, *error Loglevel: &s.logLevel, CommonCreds: s.credentials, } - connectionService.config = *config + connectionService.config = config return connectionService, nil } @@ -247,7 +247,7 @@ func (s *Skyflow) GetConnection(connId string) (*vaultutils.ConnectionConfig, *e if !exist { return nil, error.NewSkyflowError(error.INVALID_INPUT_CODE, error.CONNECTION_ID_NOT_IN_CONFIG_LIST) } - return &config.config, nil + return config.config, nil } func (s *Skyflow) GetSkyflowCredentials() *vaultutils.Credentials { @@ -352,7 +352,7 @@ func (s *Skyflow) UpdateVault(updatedConfig vaultutils.VaultConfig) *error.Skyfl func (s *Skyflow) UpdateConnection(updatedConfig vaultutils.ConnectionConfig) *error.SkyflowError { logger.Info(logs.VALIDATING_CONNECTION_CONFIG) - err := validation.ValidateConnectionConfig(updatedConfig) + err := validation.ValidateUpdateConnectionConfig(updatedConfig) if err != nil { return err } @@ -360,8 +360,19 @@ func (s *Skyflow) UpdateConnection(updatedConfig vaultutils.ConnectionConfig) *e logger.Error(fmt.Sprintf(logs.CONNECTION_CONFIG_DOES_NOT_EXIST, updatedConfig.ConnectionId)) return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.CONNECTION_ID_NOT_IN_CONFIG_LIST) } + if s.connectionServices[updatedConfig.ConnectionId].controller != nil { + // Update the credentials in the connection controller if provided + if !isCredentialsEmpty(updatedConfig.Credentials) { + s.connectionServices[updatedConfig.ConnectionId].controller.Config.Credentials = updatedConfig.Credentials + s.connectionServices[updatedConfig.ConnectionId].controller.Token = "" + s.connectionServices[updatedConfig.ConnectionId].controller.ApiKey = "" + } + if updatedConfig.ConnectionUrl != "" { + s.connectionServices[updatedConfig.ConnectionId].controller.Config.ConnectionUrl = updatedConfig.ConnectionUrl + } + } - s.connectionServices[updatedConfig.ConnectionId].config = updatedConfig + s.connectionServices[updatedConfig.ConnectionId].config = &updatedConfig return nil } @@ -451,7 +462,7 @@ func (s *Skyflow) AddConnection(config vaultutils.ConnectionConfig) *error.Skyfl return error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.CONNECTION_ID_EXISTS_IN_CONFIG_LIST) } s.connectionServices[config.ConnectionId] = &connectionService{ - config: config, + config: &config, logLevel: &s.logLevel, } logger.Info(fmt.Sprintf(logs.CONNECTION_CONTROLLER_INITIALIZED, config.ConnectionId)) @@ -544,12 +555,12 @@ func getConnectionConfig(builder map[string]*connectionService, connectionId ... if !exists { return nil, error.NewSkyflowError(error.ErrorCodesEnum(error.INVALID_INPUT_CODE), error.CONNECTION_ID_NOT_IN_CONFIG_LIST) } - return &config.config, nil + return config.config, nil } // No conenction ID passed, return the first config available for _, cfg := range builder { - return &cfg.config, nil + return cfg.config, nil } return nil, nil diff --git a/v2/client/connection_service.go b/v2/client/connection_service.go index fa678f8a..158ad223 100644 --- a/v2/client/connection_service.go +++ b/v2/client/connection_service.go @@ -9,7 +9,7 @@ import ( ) type connectionService struct { - config common.ConnectionConfig + config *common.ConnectionConfig logLevel *logger.LogLevel controller *controller.ConnectionController } diff --git a/v2/internal/constants/constants.go b/v2/internal/constants/constants.go index 5ffeb5ad..ff21d0ee 100644 --- a/v2/internal/constants/constants.go +++ b/v2/internal/constants/constants.go @@ -11,7 +11,7 @@ const ( GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer" SDK_LOG_PREFIX = "[ " + SDK_PREFIX + " ] " SDK_NAME = "Skyflow Go SDK " - SDK_VERSION = "v2.0.0-beta.1" + SDK_VERSION = "v2.0.4" SDK_PREFIX = SDK_NAME + SDK_VERSION ERROR_FROM_CLIENT = "error-from-client" REQUEST_KEY = "X-Request-Id" From 2827405730045f4c668dcade4f5ec14d73ec8e08 Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Tue, 13 Jan 2026 20:26:34 +0530 Subject: [PATCH 4/5] SK-2477 added tests --- v2/internal/validation/validations_test.go | 263 +++++++----------- .../vault/controller/controller_test.go | 31 +++ 2 files changed, 129 insertions(+), 165 deletions(-) diff --git a/v2/internal/validation/validations_test.go b/v2/internal/validation/validations_test.go index 73e7959e..4d8de5da 100644 --- a/v2/internal/validation/validations_test.go +++ b/v2/internal/validation/validations_test.go @@ -357,7 +357,7 @@ var _ = Describe("ValidateTokensForInsertRequest", func() { }) It("should return error for empty VaultId", func() { config := common.VaultConfig{ - VaultId: "id", + VaultId: "id", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: "http://demo.com", @@ -385,7 +385,7 @@ var _ = Describe("ValidateTokensForInsertRequest", func() { err := ValidateVaultConfig(config) Expect(err).To(BeNil()) }) - It("should return error for invalid url", func() { + It("should return error for invalid url", func() { config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", @@ -412,85 +412,85 @@ var _ = Describe("ValidateTokensForInsertRequest", func() { }) Describe("isValidHTTPURL", func() { - It("should return false for invalid URL (err != nil)", func() { - // This string is not a valid URL and will cause url.Parse to return an error - invalidURL := "://bad url" - config := common.VaultConfig{ + It("should return false for invalid URL (err != nil)", func() { + // This string is not a valid URL and will cause url.Parse to return an error + invalidURL := "://bad url" + config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: invalidURL, } - err := ValidateVaultConfig(config) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) - }) - It("should return false for missing scheme", func() { - invalidURL := "www.example.com" - config := common.VaultConfig{ + err := ValidateVaultConfig(config) + Expect(err).ToNot(BeNil()) + Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) + }) + It("should return false for missing scheme", func() { + invalidURL := "www.example.com" + config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: invalidURL, } - err := ValidateVaultConfig(config) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) - }) - It("should return false for unsupported scheme", func() { - invalidURL := "ftp://example.com" - config := common.VaultConfig{ + err := ValidateVaultConfig(config) + Expect(err).ToNot(BeNil()) + Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) + }) + It("should return false for unsupported scheme", func() { + invalidURL := "ftp://example.com" + config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: invalidURL, } - err := ValidateVaultConfig(config) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) - }) - It("should return false for empty host", func() { - invalidURL := "http://" - config := common.VaultConfig{ + err := ValidateVaultConfig(config) + Expect(err).ToNot(BeNil()) + Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) + }) + It("should return false for empty host", func() { + invalidURL := "http://" + config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: invalidURL, } - err := ValidateVaultConfig(config) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) + err := ValidateVaultConfig(config) + Expect(err).ToNot(BeNil()) + Expect(err.GetMessage()).To(ContainSubstring(errors.INVALID_VAULT_URL)) - }) - It("should return true for valid http URL", func() { - validURL := "http://example.com" - config := common.VaultConfig{ + }) + It("should return true for valid http URL", func() { + validURL := "http://example.com" + config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: validURL, } - err := ValidateVaultConfig(config) - Expect(err).To(BeNil()) - }) - It("should return true for valid https URL", func() { - validURL := "https://example.com" - config := common.VaultConfig{ + err := ValidateVaultConfig(config) + Expect(err).To(BeNil()) + }) + It("should return true for valid https URL", func() { + validURL := "https://example.com" + config := common.VaultConfig{ VaultId: "id", ClusterId: "cid", Env: common.PROD, Credentials: validCredentials, BaseVaultURL: validURL, } - err := ValidateVaultConfig(config) - Expect(err).To(BeNil()) - }) - }) + err := ValidateVaultConfig(config) + Expect(err).To(BeNil()) + }) + }) Context("Valid VaultConfig", func() { It("should return nil for valid VaultConfig", func() { @@ -1633,140 +1633,73 @@ var _ = Describe("ValidateTokensForInsertRequest", func() { }) }) }) - Context("ValidateGetRequest", func() { - - It("should not return error for valid Fields", func() { - getRequest := common.GetRequest{Table: "table", Ids: []string{"id1", "id2"}} - options := common.GetOptions{Fields: []string{"field1", "field2"}} - err := ValidateGetRequest(getRequest, options) - Expect(err).To(BeNil()) - }) - - It("should return error when table is empty", func() { - getRequest := common.GetRequest{Table: ""} - options := common.GetOptions{} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_TABLE)) - }) - - It("should return error when Ids is empty slice", func() { - getRequest := common.GetRequest{Table: "table", Ids: []string{}} - options := common.GetOptions{} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_IDS)) - }) - - It("should return error when Ids contains empty string", func() { - getRequest := common.GetRequest{Table: "table", Ids: []string{"", "id2"}} - options := common.GetOptions{} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_ID_IN_IDS)) - }) - - It("should return error when Fields is empty slice", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{Fields: []string{}} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_FIELDS)) - }) - - It("should return error when Fields contains empty string", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{Fields: []string{"field1", ""}} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_FIELD_IN_FIELDS)) - }) - - It("should return error when ReturnTokens is true and ColumnName is set", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ReturnTokens: true, ColumnName: "col"} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.TOKENS_GET_COLUMN_NOT_SUPPORTED)) - }) - - It("should return error when ReturnTokens is true and ColumnValues is set", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ReturnTokens: true, ColumnValues: []string{"val"}} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.TOKENS_GET_COLUMN_NOT_SUPPORTED)) + Context("ValidateUpdateVaultConfig", func() { + var validConfig common.VaultConfig + BeforeEach(func() { + validConfig = common.VaultConfig{ + VaultId: "vault1", + ClusterId: "cluster1", + Env: common.PROD, + Credentials: common.Credentials{ + ApiKey: "api-key", + }, + } }) - - It("should return error when ReturnTokens is true and RedactionType is set", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ReturnTokens: true, RedactionType: "PLAIN_TEXT"} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.REDACTION_WITH_TOKENS_NOT_SUPPORTED)) + It("should return nil for valid config", func() { + err := ValidateUpdateVaultConfig(validConfig) + Expect(err).To(BeNil()) }) - - It("should return error when neither Ids nor ColumnName/ColumnValues are set", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{} - err := ValidateGetRequest(getRequest, options) + It("should return error for empty VaultId", func() { + invalid := validConfig + invalid.VaultId = "" + err := ValidateUpdateVaultConfig(invalid) Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.UNIQUE_COLUMN_OR_IDS_KEY_ERROR)) }) - - It("should return error when both Ids and ColumnName/ColumnValues are set", func() { - getRequest := common.GetRequest{Table: "table", Ids: []string{"id1"}} - options := common.GetOptions{ColumnName: "col", ColumnValues: []string{"val"}} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.BOTH_IDS_AND_COLUMN_DETAILS_SPECIFIED)) + It("should return error for empty ClusterId", func() { + invalid := validConfig + invalid.ClusterId = "" + err := ValidateUpdateVaultConfig(invalid) + Expect(err).To(BeNil()) }) - - It("should return error when ColumnValues is set but ColumnName is empty", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ColumnValues: []string{"val"}} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.COLUMN_NAME_KEY_ERROR)) + It("should return error for invalid Credentials", func() { + invalid := validConfig + invalid.Credentials = common.Credentials{} + err := ValidateUpdateVaultConfig(invalid) + Expect(err).To(BeNil()) }) - - It("should return error when ColumnName is set but ColumnValues is nil", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ColumnName: "col"} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_COLUMN_VALUES)) + }) + Context("ValidateUpdateConnectionConfig", func() { + var validConfig common.ConnectionConfig + BeforeEach(func() { + validConfig = common.ConnectionConfig{ + ConnectionId: "conn1", + ConnectionUrl: "https://conn1.com", + Credentials: common.Credentials{ + ApiKey: "api-key", + }, + } }) - - It("should return error when ColumnName is set and ColumnValues is empty slice", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ColumnName: "col", ColumnValues: []string{}} - err := ValidateGetRequest(getRequest, options) - Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_COLUMN_VALUES)) + It("should return nil for valid config", func() { + err := ValidateUpdateConnectionConfig(validConfig) + Expect(err).To(BeNil()) }) - - It("should return error when ColumnName is set and ColumnValues contains empty string", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ColumnName: "col", ColumnValues: []string{"val1", ""}} - err := ValidateGetRequest(getRequest, options) + It("should return error for empty ConnectionId", func() { + invalid := validConfig + invalid.ConnectionId = "" + err := ValidateUpdateConnectionConfig(invalid) Expect(err).ToNot(BeNil()) - Expect(err.GetMessage()).To(ContainSubstring(errors.EMPTY_VALUE_IN_COLUMN_VALUES)) }) - - It("should not return error for valid Ids", func() { - getRequest := common.GetRequest{Table: "table", Ids: []string{"id1", "id2"}} - options := common.GetOptions{} - err := ValidateGetRequest(getRequest, options) + It("should return error for empty ConnectionUrl", func() { + invalid := validConfig + invalid.ConnectionUrl = "" + err := ValidateUpdateConnectionConfig(invalid) Expect(err).To(BeNil()) }) - - It("should not return error for valid ColumnName and ColumnValues", func() { - getRequest := common.GetRequest{Table: "table"} - options := common.GetOptions{ColumnName: "col", ColumnValues: []string{"val1", "val2"}} - err := ValidateGetRequest(getRequest, options) + It("should return error for invalid Credentials", func() { + invalid := validConfig + invalid.Credentials = common.Credentials{} + err := ValidateUpdateConnectionConfig(invalid) Expect(err).To(BeNil()) }) - }) }) diff --git a/v2/internal/vault/controller/controller_test.go b/v2/internal/vault/controller/controller_test.go index 03a810a0..1f3bae3c 100644 --- a/v2/internal/vault/controller/controller_test.go +++ b/v2/internal/vault/controller/controller_test.go @@ -1506,6 +1506,37 @@ var _ = Describe("VaultController", func() { err := SetBearerTokenForVaultController(vaultController) Expect(err).ToNot(BeNil()) }) + It("should generate token if apikey string is provided", func() { + vaultController.Token = "" + vaultController.Config.Credentials.Path = "" + vaultController.Config.Credentials.ApiKey = os.Getenv("API_KEY") + vaultController.Config.Credentials.CredentialsString = "" + + err := SetBearerTokenForVaultController(vaultController) + Expect(err).To(BeNil()) + }) + It("should generate token if apikey string is provided", func() { + vaultController.Token = "" + vaultController.Config.Credentials.Path = "" + vaultController.Config.Credentials.ApiKey = "" + vaultController.Config.Credentials.CredentialsString = "" + + err := SetBearerTokenForVaultController(vaultController) + Expect(err).ToNot(BeNil()) + }) + It("should generate token if apikey string is provided", func() { + vaultController.Token = "" + vaultController.Config.Credentials.Path = "" + vaultController.Config.Credentials.ApiKey = "" + vaultController.Config.Credentials.CredentialsString = "" + vaultController.CommonCreds = &Credentials{ + ApiKey: os.Getenv("API_KEY"), + } + + err := SetBearerTokenForVaultController(vaultController) + Expect(err).To(BeNil()) + }) + }) Context("CreateRequestClient", func() { From e19de082b092fb7bf37293805e01fe245cd25af8 Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Tue, 13 Jan 2026 20:31:38 +0530 Subject: [PATCH 5/5] SK-2477 remove commented code --- v2/client/client.go | 25 ------------------------- v2/internal/validation/validations.go | 8 -------- 2 files changed, 33 deletions(-) diff --git a/v2/client/client.go b/v2/client/client.go index 9b319ba6..353d6f87 100644 --- a/v2/client/client.go +++ b/v2/client/client.go @@ -145,15 +145,6 @@ func (s *Skyflow) Vault(vaultID ...string) (*vaultService, *error.SkyflowError) if err != nil { return nil, err } - - // err = setVaultCredentials(config, s.credentials) - // if err != nil { - // return nil, err - // } - // err1 := validation.ValidateVaultConfig(*config) - // if err1 != nil { - // return nil, err1 - // } vaultService := &vaultService{} // Get the VaultController from the builder's vaultServices map vaultService, exists := s.vaultServices[config.VaultId] @@ -177,14 +168,6 @@ func (s *Skyflow) Connection(connectionId ...string) (*connectionService, *error if err != nil { return nil, err } - // err = setConnectionCredentials(config, s.credentials) - // if err != nil { - // return nil, err - // } - // err1 := validation.ValidateConnectionConfig(*config) - // if err1 != nil { - // return nil, err1 - // } connectionService := &connectionService{} connectionService, exists := s.connectionServices[config.ConnectionId] if !exists { @@ -208,14 +191,6 @@ func (s *Skyflow) Detect(vaultID ...string) (*detectService, *error.SkyflowError return nil, err } - // err = setDetectCredentials(config, s.credentials) - // if err != nil { - // return nil, err - // } - // err1 := validation.ValidateVaultConfig(*config) - // if err1 != nil { - // return nil, err1 - // } detectService := &detectService{} // Get the VaultController from the builder's vaultServices map detectService, exists := s.detectServices[config.VaultId] diff --git a/v2/internal/validation/validations.go b/v2/internal/validation/validations.go index b19d1eba..92942806 100644 --- a/v2/internal/validation/validations.go +++ b/v2/internal/validation/validations.go @@ -503,10 +503,6 @@ func ValidateUpdateVaultConfig(vaultConfig common.VaultConfig) *skyflowError.Sky logger.Error(logs.VAULT_ID_IS_REQUIRED) return skyflowError.NewSkyflowError(skyflowError.INVALID_INPUT_CODE, skyflowError.INVALID_VAULT_ID) } - // err := ValidateCredentials(vaultConfig.Credentials) - // if err != nil { - // return err - // } return nil } @@ -544,10 +540,6 @@ func ValidateUpdateConnectionConfig(config common.ConnectionConfig) *skyflowErro return skyflowError.NewSkyflowError(skyflowError.INVALID_INPUT_CODE, skyflowError.INVALID_CONNECTION_URL) } } - // err := ValidateCredentials(config.Credentials) - // if err != nil { - // return err - // } return nil }