Skip to content

Commit

Permalink
Add token argument to provider to facilitate token based auth
Browse files Browse the repository at this point in the history
Signed-off-by: Dom Del Nano <[email protected]>
  • Loading branch information
ddelnano committed Mar 13, 2024
1 parent e77ee5b commit e31a949
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
37 changes: 29 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type Config struct {
Url string
Username string
Password string
Token string
InsecureSkipVerify bool
RetryMode RetryMode
RetryMaxTime time.Duration
Expand All @@ -164,6 +165,7 @@ func GetConfigFromEnv() Config {
var wsURL string
var username string
var password string
var token string
insecure := false
retryMode := None
retryMaxTime := 5 * time.Minute
Expand All @@ -176,6 +178,9 @@ func GetConfigFromEnv() Config {
if v := os.Getenv("XOA_PASSWORD"); v != "" {
password = v
}
if v := os.Getenv("XOA_TOKEN"); v != "" {
token = v
}
if v := os.Getenv("XOA_INSECURE"); v != "" {
insecure = true
}
Expand All @@ -199,6 +204,7 @@ func GetConfigFromEnv() Config {
Url: wsURL,
Username: username,
Password: password,
Token: token,
InsecureSkipVerify: insecure,
RetryMode: retryMode,
RetryMaxTime: retryMaxTime,
Expand All @@ -209,6 +215,16 @@ func NewClient(config Config) (XOClient, error) {
wsURL := config.Url
username := config.Username
password := config.Password
token := config.Token

if token == "" && (username == "" || password == "") {
return nil, fmt.Errorf("One of the following environment variable(s) must be set: XOA_USER and XOA_PASSWORD or XOA_TOKEN")
}

useTokenAuth := false
if token != "" {
useTokenAuth = true
}

tlsConfig := &tls.Config{
InsecureSkipVerify: config.InsecureSkipVerify,
Expand All @@ -226,20 +242,25 @@ func NewClient(config Config) (XOClient, error) {
h = &handler{}
c := jsonrpc2.NewConn(context.Background(), objStream, h)

reqParams := map[string]interface{}{
"email": username,
"password": password,
reqParams := map[string]interface{}{}
if useTokenAuth {
reqParams["token"] = token
} else {

reqParams["email"] = username
reqParams["password"] = password
}
var reply signInResponse
err = c.Call(context.Background(), "session.signInWithPassword", reqParams, &reply)
err = c.Call(context.Background(), "session.signIn", reqParams, &reply)
if err != nil {
return nil, err
}

var token string
err = c.Call(context.Background(), "token.create", map[string]interface{}{}, &token)
if err != nil {
return nil, err
if !useTokenAuth {
err = c.Call(context.Background(), "token.create", map[string]interface{}{}, &token)
if err != nil {
return nil, err
}
}

jar, err := cookiejar.New(&cookiejar.Options{})
Expand Down
10 changes: 4 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ provider "xenorchestra" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `password` (String) Password for xoa api. Can be set via the XOA_PASSWORD environment variable.
- `url` (String) Hostname of the xoa router. Can be set via the XOA_URL environment variable.
- `username` (String) User account for xoa api. Can be set via the XOA_USER environment variable.

### Optional

- `insecure` (Boolean) Whether SSL should be verified or not. Can be set via the XOA_INSECURE environment variable.
- `password` (String) Password for xoa api. Can be set via the XOA_PASSWORD environment variable.
- `retry_max_time` (String) If `retry_mode` is set, this specifies the duration for which the backoff method will continue retries. Can be set via the `XOA_RETRY_MAX_TIME` environment variable
- `retry_mode` (String) Specifies if retries should be attempted for requests that require eventual . Can be set via the XOA_RETRY_MODE environment variable.
- `token` (String) Password for xoa api. Can be set via the XOA_TOKEN environment variable.
- `url` (String) Hostname of the xoa router. Can be set via the XOA_URL environment variable.
- `username` (String) User account for xoa api. Can be set via the XOA_USER environment variable.
29 changes: 21 additions & 8 deletions xoa/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@ func Provider() *schema.Provider {
Description: "Hostname of the xoa router. Can be set via the XOA_URL environment variable.",
},
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("XOA_USER", nil),
Description: "User account for xoa api. Can be set via the XOA_USER environment variable.",
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("XOA_USER", nil),
Description: "User account for xoa api. Can be set via the XOA_USER environment variable.",
RequiredWith: []string{"password"},
ConflictsWith: []string{"token"},
},
"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("XOA_PASSWORD", nil),
Description: "Password for xoa api. Can be set via the XOA_PASSWORD environment variable.",
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("XOA_PASSWORD", nil),
Description: "Password for xoa api. Can be set via the XOA_PASSWORD environment variable.",
RequiredWith: []string{"username"},
ConflictsWith: []string{"token"},
},
"token": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("XOA_TOKEN", nil),
Description: "Password for xoa api. Can be set via the XOA_TOKEN environment variable.",
ConflictsWith: []string{"username", "password"},
},
"insecure": &schema.Schema{
Type: schema.TypeBool,
Expand Down Expand Up @@ -92,6 +103,7 @@ func xoaConfigure(d *schema.ResourceData) (interface{}, error) {
url := d.Get("url").(string)
username := d.Get("username").(string)
password := d.Get("password").(string)
token := d.Get("token").(string)
insecure := d.Get("insecure").(bool)
retryMode := d.Get("retry_mode").(string)
retryMaxTime := d.Get("retry_max_time").(string)
Expand All @@ -110,6 +122,7 @@ func xoaConfigure(d *schema.ResourceData) (interface{}, error) {
Url: url,
Username: username,
Password: password,
Token: token,
InsecureSkipVerify: insecure,
RetryMode: retry,
RetryMaxTime: duration,
Expand Down
13 changes: 8 additions & 5 deletions xoa/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("XOA_URL"); v == "" {
t.Fatal("The XOA_URL environment variable must be set")
}
if v := os.Getenv("XOA_USER"); v == "" {
t.Fatal("The XOA_USER environment variable must be set")
}
if v := os.Getenv("XOA_PASSWORD"); v == "" {
t.Fatal("The XOA_PASSWORD environment variable must be set")

user := os.Getenv("XOA_USER")
password := os.Getenv("XOA_PASSWORD")
token := os.Getenv("XOA_TOKEN")

if token == "" && (user == "" || password == "") {
t.Fatal("One of the following environment variable(s) must be set: XOA_USER and XOA_PASSWORD or XOA_TOKEN")
}

if v := os.Getenv("XOA_POOL"); v == "" {
t.Fatal("The XOA_POOL environment variable must be set")
}
Expand Down

0 comments on commit e31a949

Please sign in to comment.