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

feat: add writing secrets feature in terraform provider #89

Merged
merged 4 commits into from
Nov 26, 2024
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.32.0
github.com/joho/godotenv v1.5.1
go.uber.org/zap v1.27.0
Expand All @@ -21,7 +22,7 @@ require (
)

require (
github.com/BeyondTrust/go-client-library-passwordsafe v0.9.0
github.com/BeyondTrust/go-client-library-passwordsafe v0.10.0
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/BeyondTrust/go-client-library-passwordsafe v0.9.0 h1:7uY74cRTycrdmae/6Jm46Ji0NLcEWfmk8gWNb5+LVZA=
github.com/BeyondTrust/go-client-library-passwordsafe v0.9.0/go.mod h1:TnbBwWYg9rtfDxQGF7pmD0gCPcbWgCUQIqum3dFMRTk=
github.com/BeyondTrust/go-client-library-passwordsafe v0.10.0 h1:dtjvXamv1KjC9NZGLWTT7Avddpj6x8DQ7lZs19CVwQk=
github.com/BeyondTrust/go-client-library-passwordsafe v0.10.0/go.mod h1:72FMrpiz1fUSiIIIAXiCzQ55Y83spsu2jl5n/Stzfks=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
Expand Down Expand Up @@ -34,6 +34,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
Expand Down
118 changes: 118 additions & 0 deletions provider/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2023 BeyondTrust. All rights reserved.
// Package Provider implements a terraform provider that can talk with Beyondtrust Secret Safe API.
package provider

import (
"fmt"
"sync/atomic"

auth "github.com/BeyondTrust/go-client-library-passwordsafe/api/authentication"
"github.com/BeyondTrust/go-client-library-passwordsafe/api/entities"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// autenticate get Password Safe authentication.
func autenticate(d *schema.ResourceData, m interface{}) (entities.SignApinResponse, error) {
authenticationObj := m.(*auth.AuthenticationObj)
var err error

mu.Lock()
if atomic.LoadUint64(&signInCount) > 0 {
atomic.AddUint64(&signInCount, 1)
zapLogger.Debug(fmt.Sprintf("%v %v", "Already signed in", atomic.LoadUint64(&signInCount)))
mu.Unlock()

} else {
signApinResponse, err = authenticationObj.GetPasswordSafeAuthentication()
if err != nil {
mu.Unlock()
zapLogger.Error(err.Error())
return entities.SignApinResponse{}, err
}
atomic.AddUint64(&signInCount, 1)
zapLogger.Debug(fmt.Sprintf("%v %v", "signin", atomic.LoadUint64(&signInCount)))
mu.Unlock()
}

return signApinResponse, nil
}

// signOut sign Password Safe out
func signOut(d *schema.ResourceData, m interface{}) error {
authenticationObj := m.(*auth.AuthenticationObj)
var err error

mu_out.Lock()
if atomic.LoadUint64(&signInCount) > 1 {
zapLogger.Debug(fmt.Sprintf("%v %v", "Ignore signout", atomic.LoadUint64(&signInCount)))
// decrement counter, don't signout.
atomic.AddUint64(&signInCount, ^uint64(0))
mu_out.Unlock()
} else {
err = authenticationObj.SignOut()
if err != nil {
return err
}
zapLogger.Debug(fmt.Sprintf("%v %v", "signout user", atomic.LoadUint64(&signInCount)))
// decrement counter
atomic.AddUint64(&signInCount, ^uint64(0))
mu_out.Unlock()

}

return nil
}

// getOwnersSchema get Owners schema.
func getOwnersSchema() *schema.Schema {

schema := schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"owner_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"owner": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"email": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
}

return &schema
}

// getUrlsSchema get Urls schema.
func getUrlsSchema() *schema.Schema {

schema := schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"credential_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
}

return &schema
}
Loading
Loading