1.0.0
This commit is contained in:
178
internal/provider/provider_validation.go
Normal file
178
internal/provider/provider_validation.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
sdk_config "repository.basistech.ru/BASIS/decort-golang-sdk/config"
|
||||
)
|
||||
|
||||
// dynamixProviderConfig helps organize provider validation
|
||||
type dynamixProviderConfig struct {
|
||||
authenticator string
|
||||
oauth2Url string
|
||||
controllerUrl string
|
||||
user string
|
||||
password string
|
||||
bvsUser string
|
||||
bvsPassword string
|
||||
domain string
|
||||
appId string
|
||||
appSecret string
|
||||
jwt string
|
||||
allowUnverifiedSsl bool
|
||||
pathConfig string
|
||||
pathToken string
|
||||
timeToRefresh int64
|
||||
token sdk_config.Token
|
||||
cc_client *http.Client
|
||||
}
|
||||
|
||||
// new sets up default values, values from env and save user provided values for decort provider into decortConfig:
|
||||
// authenticator, oauth2Url, controllerUrl transformed to lowercase;
|
||||
// oauth2Url, user, password, bvsUser, bvsPassword, domain, appId, appSecret, jwt uploaded from env if not provided;
|
||||
// allowUnverifiedSsl default value set as false.
|
||||
func (d *dynamixProviderConfig) new(config dynamixProviderModel) {
|
||||
d.authenticator = strings.ToLower(config.Authenticator.ValueString())
|
||||
|
||||
if config.Oauth2Url.IsUnknown() {
|
||||
d.oauth2Url = os.Getenv("DECORT_OAUTH2_URL")
|
||||
} else {
|
||||
d.oauth2Url = config.Oauth2Url.ValueString()
|
||||
}
|
||||
d.oauth2Url = strings.ToLower(d.oauth2Url)
|
||||
|
||||
d.controllerUrl = strings.ToLower(config.ControllerUrl.ValueString())
|
||||
if d.controllerUrl == "" {
|
||||
log.Debugf("empty DECORT cloud controller URL provided")
|
||||
return
|
||||
}
|
||||
|
||||
if config.User.IsUnknown() {
|
||||
d.user = os.Getenv("DECORT_USER")
|
||||
} else {
|
||||
d.user = config.User.ValueString()
|
||||
}
|
||||
|
||||
if config.Password.IsUnknown() {
|
||||
d.password = os.Getenv("DECORT_PASSWORD")
|
||||
} else {
|
||||
d.password = config.Password.ValueString()
|
||||
}
|
||||
|
||||
if config.BvsUser.IsUnknown() {
|
||||
d.bvsUser = os.Getenv("DECORT_BVS_USER")
|
||||
} else {
|
||||
d.bvsUser = config.BvsUser.ValueString()
|
||||
}
|
||||
|
||||
if config.BvsPassword.IsUnknown() {
|
||||
d.bvsPassword = os.Getenv("DECORT_BVS_PASSWORD")
|
||||
} else {
|
||||
d.bvsPassword = config.BvsPassword.ValueString()
|
||||
}
|
||||
|
||||
if config.Domain.IsUnknown() {
|
||||
d.domain = os.Getenv("DECORT_DOMAIN")
|
||||
} else {
|
||||
d.domain = config.Domain.ValueString()
|
||||
}
|
||||
|
||||
if config.AppId.IsUnknown() {
|
||||
d.appId = os.Getenv("DECORT_APP_ID")
|
||||
} else {
|
||||
d.appId = config.AppId.ValueString()
|
||||
}
|
||||
|
||||
if config.AppSecret.IsUnknown() {
|
||||
d.appSecret = os.Getenv("DECORT_APP_SECRET")
|
||||
} else {
|
||||
d.appSecret = config.AppSecret.ValueString()
|
||||
}
|
||||
|
||||
if config.Jwt.IsUnknown() {
|
||||
d.jwt = os.Getenv("DECORT_JWT")
|
||||
} else {
|
||||
d.jwt = config.Jwt.ValueString()
|
||||
}
|
||||
|
||||
if config.AllowUnverifiedSsl.IsUnknown() {
|
||||
d.allowUnverifiedSsl = false // default false
|
||||
} else {
|
||||
d.allowUnverifiedSsl = config.AllowUnverifiedSsl.ValueBool()
|
||||
}
|
||||
|
||||
if !config.PathConfig.IsUnknown() {
|
||||
d.pathConfig = config.PathConfig.ValueString()
|
||||
}
|
||||
|
||||
if !config.PathToken.IsUnknown() {
|
||||
d.pathToken = config.PathToken.ValueString()
|
||||
}
|
||||
|
||||
if !config.TimeToRefresh.IsUnknown() {
|
||||
d.timeToRefresh = config.TimeToRefresh.ValueInt64()
|
||||
}
|
||||
|
||||
d.token = sdk_config.Token{}
|
||||
}
|
||||
|
||||
// validateAuthenticator validates authenticator and other parameters from provider configuration provided by user.
|
||||
// If successful, the mode and nil is returned. If unsuccessful for any
|
||||
// reason, the method will return mode = MODE_UNDEF and error.
|
||||
func (d *dynamixProviderConfig) validateAuthenticator() (int, error) {
|
||||
var mode = MODE_UNDEF
|
||||
switch d.authenticator {
|
||||
case "jwt":
|
||||
if d.jwt == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'jwt' specified but no JWT provided")
|
||||
}
|
||||
mode = MODE_JWT
|
||||
case "decs3o":
|
||||
if d.oauth2Url == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'decs3o' specified but no OAuth2 URL provided")
|
||||
}
|
||||
if d.appId == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'decs3o' specified but no Application ID provided")
|
||||
}
|
||||
if d.appSecret == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'decs3o' specified but no Secret ID provided")
|
||||
}
|
||||
mode = MODE_DECS3O
|
||||
case "legacy":
|
||||
if d.user == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'legacy' specified but no user provided")
|
||||
}
|
||||
if d.password == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'legacy' specified but no password provided")
|
||||
}
|
||||
mode = MODE_LEGACY
|
||||
case "bvs":
|
||||
if d.bvsUser == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'bvs' specified but no bvs user provided")
|
||||
}
|
||||
if d.bvsPassword == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'bvs' specified but no bvs password provided")
|
||||
}
|
||||
if d.oauth2Url == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'bvs' specified but no bvs URL provided")
|
||||
}
|
||||
if d.appId == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'bvs' specified but no Application ID provided")
|
||||
}
|
||||
if d.appSecret == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'bvs' specified but no Secret ID provided")
|
||||
}
|
||||
if d.domain == "" {
|
||||
return mode, fmt.Errorf("authenticator mode 'bvs' specified but no Domain provided")
|
||||
}
|
||||
mode = MODE_BVS
|
||||
default:
|
||||
return mode, fmt.Errorf("unknown authenticator mode %q provided", d.authenticator)
|
||||
}
|
||||
|
||||
return mode, nil
|
||||
}
|
||||
Reference in New Issue
Block a user