Skip to content

Commit

Permalink
add partial credentials validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmdarroch committed Aug 30, 2022
1 parent 92ab62f commit 20968ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/hcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ func (c *hcpConfig) SCADATLSConfig() *tls.Config {

func (c *hcpConfig) validate() error {

// Ensure both client credentials provided
if (c.clientCredentialsConfig.ClientID == "" && c.clientCredentialsConfig.ClientSecret != "") ||
(c.clientCredentialsConfig.ClientID != "" && c.clientCredentialsConfig.ClientSecret == "") {
return fmt.Errorf("both client ID and secret must be provided")
}

// Ensure at least one auth method configured
if c.clientCredentialsConfig.ClientID == "" && c.clientCredentialsConfig.ClientSecret == "" && c.oauth2Config.ClientID == "" {
return fmt.Errorf("either client credentials or oauth2 client ID must be provided")
}

// Ensure the auth URL is valid
if c.authURL.Host == "" {
return fmt.Errorf("the auth URL has to be non-empty")
Expand Down
7 changes: 7 additions & 0 deletions config/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func TestNew_Invalid(t *testing.T) {
options []HCPConfigOption
expectedError string
}{
{
name: "partial credentials",
options: []HCPConfigOption{
WithClientCredentials("my-client-id", ""),
},
expectedError: "the configuration is not valid: both client ID and secret must be provided",
},
{
name: "empty portal URL",
options: []HCPConfigOption{
Expand Down

0 comments on commit 20968ce

Please sign in to comment.