Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to connect using OTP 2FA #69

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export PM_API_URL="https://xxxx.com:8006/api2/json"
export PM_USER=user@pam
export PM_PASS=password
```

If a 2FA OTP code is required
```bash
# Optional 2FA OTP code
export PM_OTP=otpcode
```

## Run

Expand All @@ -58,6 +62,8 @@ provider "proxmox" {
pm_api_url = "https://proxmox-server01.example.com:8006/api2/json"
pm_password = "secret"
pm_user = "terraform-user@pve"
//Optional
pm_otp = "otpcode"
*/
}

Expand Down
1 change: 1 addition & 0 deletions examples/cloudinit_example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ provider "proxmox" {
pm_api_url = "https://proxmox-server01.example.com:8006/api2/json"
pm_password = "secret"
pm_user = "terraform-user@pve"
pm_otp = ""
}

resource "proxmox_vm_qemu" "cloudinit-test" {
Expand Down
1 change: 1 addition & 0 deletions examples/lxc_example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ provider "proxmox" {
pm_api_url = "https://proxmox.org/api2/json"
pm_password = "supersecret"
pm_user = "terraform-user@pve"
pm_otp = ""
}

resource "proxmox_lxc" "lxc-test" {
Expand Down
12 changes: 9 additions & 3 deletions proxmox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func Provider() *schema.Provider {
Optional: true,
Default: false,
},
"pm_otp": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PM_OTP", nil),
Description: "OTP 2FA code (if required)",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand All @@ -69,7 +75,7 @@ func Provider() *schema.Provider {
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
client, err := getClient(d.Get("pm_api_url").(string), d.Get("pm_user").(string), d.Get("pm_password").(string), d.Get("pm_tls_insecure").(bool))
client, err := getClient(d.Get("pm_api_url").(string), d.Get("pm_user").(string), d.Get("pm_password").(string), d.Get("pm_otp").(string), d.Get("pm_tls_insecure").(bool))
if err != nil {
return nil, err
}
Expand All @@ -84,13 +90,13 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}, nil
}

func getClient(pm_api_url string, pm_user string, pm_password string, pm_tls_insecure bool) (*pxapi.Client, error) {
func getClient(pm_api_url string, pm_user string, pm_password string, pm_otp string, pm_tls_insecure bool) (*pxapi.Client, error) {
tlsconf := &tls.Config{InsecureSkipVerify: true}
if !pm_tls_insecure {
tlsconf = nil
}
client, _ := pxapi.NewClient(pm_api_url, nil, tlsconf)
err := client.Login(pm_user, pm_password)
err := client.Login(pm_user, pm_password, pm_otp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion proxmox/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func applyFn(ctx context.Context) error {
vmr.SetNode(targetNode)
client := currentClient
if client == nil {
client, err = getClient(connInfo["pm_api_url"], connInfo["pm_user"], connInfo["pm_password"], connInfo["pm_tls_insecure"] == "true")
client, err = getClient(connInfo["pm_api_url"], connInfo["pm_user"], connInfo["pm_password"], connInfo["pm_otp"], connInfo["pm_tls_insecure"] == "true")
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ func initConnInfo(
"pm_api_url": client.ApiUrl,
"pm_user": client.Username,
"pm_password": client.Password,
"pm_otp": client.Otp,
"pm_tls_insecure": "true", // TODO - pass pm_tls_insecure state around, but if we made it this far, default insecure
})
return nil
Expand Down