Skip to content

Commit

Permalink
hash systemuser password in state
Browse files Browse the repository at this point in the history
  • Loading branch information
ravager-dk committed Apr 27, 2023
1 parent 4f8dc1b commit 25e3b2d
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions citrixadc/resource_citrixadc_systemuser.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package citrixadc

import (
"strings"

"github.com/citrix/adc-nitro-go/resource/config/system"
"github.com/citrix/adc-nitro-go/service"

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"bytes"
"crypto/sha512"
"encoding/hex"
"fmt"
"log"
"strconv"
Expand Down Expand Up @@ -40,10 +44,11 @@ func resourceCitrixAdcSystemuser() *schema.Resource {
Computed: true,
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: false,
Sensitive: true,
Type: schema.TypeString,
Optional: true,
Computed: false,
Sensitive: true,
DiffSuppressFunc: ignoreHashMatch,
},
"hashedpassword": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -90,13 +95,25 @@ func resourceCitrixAdcSystemuser() *schema.Resource {
}
}

func hashPassword(password string) string {
hash := sha512.Sum512([]byte(password))
return hex.EncodeToString(hash[:])
}

func ignoreHashMatch(k, old, new string, d *schema.ResourceData) bool {
oldStr := strings.ToLower(old)
newStr := strings.ToLower(hashPassword(new))
log.Printf("[DEBUG] comparing old value: %s with new value %s", oldStr, newStr)
return oldStr == newStr
}

func createSystemuserFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createSystemuserFunc")
client := meta.(*NetScalerNitroClient).client
login_username := (*meta.(*NetScalerNitroClient)).Username
username := d.Get("username").(string)

if (username == login_username) {
if username == login_username {
return fmt.Errorf("It seems you are trying to change the password of the Admin user. If so, please use the resource \"citrixadc_change_password\"")
}
systemuser := system.Systemuser{
Expand All @@ -120,7 +137,7 @@ func createSystemuserFunc(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(username)

d.Set("password", hashPassword(d.Get("password").(string)))
err = readSystemuserFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this systemuser but we can't read it ?? %s", username)
Expand Down Expand Up @@ -224,6 +241,7 @@ func updateSystemuserFunc(d *schema.ResourceData, meta interface{}) error {
return err
}
}
d.Set("password", hashPassword(d.Get("password").(string)))
return readSystemuserFunc(d, meta)
}

Expand Down

0 comments on commit 25e3b2d

Please sign in to comment.