Skip to content

Commit

Permalink
Merge pull request #787 from timofurrer/feature/early-cred-fail
Browse files Browse the repository at this point in the history
Support `early_auth_check` flag in provider config. Closes #777
  • Loading branch information
nagyv committed Jan 24, 2022
2 parents 28c19f9 + c650fc4 commit b4d4f8d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ The following arguments are supported in the `provider` block:
* `client_cert` - (Optional) File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.

* `client_key` - (Optional) File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data. Required when `client_cert` is set.

* `early_auth_check` - (Optional) (experimental) By default the provider does a dummy request to get the current user in order
to verify that the provider configuration is correct and the GitLab API is reachable.
Turn it off, to skip this check. This may be useful if the GitLab instance does not yet exist and is created within the same terraform module.
This is an experimental feature and may change in the future. Please make sure to always keep backups of your state.
17 changes: 10 additions & 7 deletions gitlab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (

// Config is per-provider, specifies where to connect to gitlab
type Config struct {
Token string
BaseURL string
Insecure bool
CACertFile string
ClientCert string
ClientKey string
Token string
BaseURL string
Insecure bool
CACertFile string
ClientCert string
ClientKey string
EarlyAuthFail bool
}

// Client returns a *gitlab.Client to interact with the configured gitlab instance
Expand Down Expand Up @@ -75,7 +76,9 @@ func (c *Config) Client() (*gitlab.Client, error) {
}

// Test the credentials by checking we can get information about the authenticated user.
_, _, err = client.Users.CurrentUser()
if c.EarlyAuthFail {
_, _, err = client.Users.CurrentUser()
}

return client, err
}
21 changes: 15 additions & 6 deletions gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func Provider() *schema.Provider {
Default: "",
Description: descriptions["client_key"],
},
"early_auth_check": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: descriptions["early_auth_check"],
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -127,17 +133,20 @@ func init() {
"client_cert": "File path to client certificate when GitLab instance is behind company proxy. File must contain PEM encoded data.",

"client_key": "File path to client key when GitLab instance is behind company proxy. File must contain PEM encoded data.",

"early_auth_check": "Try to authenticate with the `CurrentUser` endpoint during the provider initialization. (experimental, see docs)",
}
}

func providerConfigure(ctx context.Context, p *schema.Provider, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
config := Config{
Token: d.Get("token").(string),
BaseURL: d.Get("base_url").(string),
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
ClientCert: d.Get("client_cert").(string),
ClientKey: d.Get("client_key").(string),
Token: d.Get("token").(string),
BaseURL: d.Get("base_url").(string),
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
ClientCert: d.Get("client_cert").(string),
ClientKey: d.Get("client_key").(string),
EarlyAuthFail: d.Get("early_auth_check").(bool),
}

client, err := config.Client()
Expand Down

0 comments on commit b4d4f8d

Please sign in to comment.