Skip to content

Commit

Permalink
enable bearer token auth
Browse files Browse the repository at this point in the history
fixes goharbor#383
this is largely a copy/paste of goharbor#384 by @ilyesAj
with the changes requested by @flbla
  • Loading branch information
robert lestak committed Feb 6, 2024
1 parent 9f2dc61 commit 53ff1bf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
32 changes: 20 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ import (
)

type Client struct {
url string
username string
password string
insecure bool
httpClient *http.Client
url string
username string
password string
bearerToken string
insecure bool
httpClient *http.Client
}

// NewClient creates common settings
func NewClient(url string, username string, password string, insecure bool) *Client {
func NewClient(url string, username string, password string, bearerToken string, insecure bool) *Client {

return &Client{
url: url,
username: username,
password: password,
insecure: insecure,
httpClient: &http.Client{},
url: url,
username: username,
password: password,
bearerToken: bearerToken,
insecure: insecure,
httpClient: &http.Client{},
}
}

Expand Down Expand Up @@ -56,7 +58,13 @@ func (c *Client) SendRequest(method string, path string, payload interface{}, st
return "", "", 0, err
}

req.SetBasicAuth(c.username, c.password)
// Use access token authentification if bearer Token is specified
if c.bearerToken != "" {
req.Header.Add("Authorization", "Bearer "+c.bearerToken)
} else {
req.SetBasicAuth(c.username, c.password)
}

req.Header.Add("Content-Type", "application/json")

resp, err := client.Do(req)
Expand Down
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ provider "harbor" {
url = "https://harbor.aceme_corpartion.com"
username = "insert_admin_username_here"
password = "insert_password_here"
bearer_token = "insert_bearer_token_here"
}
```

Expand All @@ -34,6 +35,7 @@ Alternatively, these environment variables can be used to set the provider confi
HARBOR_URL
HARBOR_USERNAME
HARBOR_PASSWORD
HARBOR_BEARER_TOKEN
HARBOR_IGNORE_CERT
```

Expand All @@ -43,5 +45,6 @@ The following arguments are supported:
* **url** - (Required) The url of harbor
* **username** - (Required) The username to be used to access harbor
* **password** - (Required) The password to be used to access harbor
* **bearer_token** - (Optional) The bearer token to be used to access harbor. Will take precedence over username and password if set
* **insecure** - (Optional) Choose to ignore certificate errors
* **api_version** - (Optional) Choose which version of the api you would like to use 1 or 2. Default is `2`
8 changes: 7 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HARBOR_PASSWORD", ""),
},
"bearer_token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("HARBOR_BEARER_TOKEN", ""),
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -88,6 +93,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {

username := d.Get("username").(string)
password := d.Get("password").(string)
bearerToken := d.Get("bearer_token").(string)
insecure := d.Get("insecure").(bool)
apiVersion := d.Get("api_version").(int)

Expand All @@ -101,5 +107,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
apiPath = "/api/v2.0"
}

return client.NewClient(url+apiPath, username, password, insecure), nil
return client.NewClient(url+apiPath, username, password, bearerToken, insecure), nil
}

0 comments on commit 53ff1bf

Please sign in to comment.