/
datasource_vaultsecret.go
125 lines (113 loc) · 3.09 KB
/
datasource_vaultsecret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package centrify
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
logger "github.com/marcozj/golang-sdk/logging"
vault "github.com/marcozj/golang-sdk/platform"
"github.com/marcozj/golang-sdk/restapi"
)
func dataSourceSecret_deprecated() *schema.Resource {
return &schema.Resource{
Read: dataSourceSecretRead,
Schema: getDSSecretSchema(),
DeprecationMessage: "dataresource centrifyvault_vaultsecret is deprecated will be removed in the future, use centrify_secret instead",
}
}
func dataSourceSecret() *schema.Resource {
return &schema.Resource{
Read: dataSourceSecretRead,
Schema: getDSSecretSchema(),
}
}
func getDSSecretSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"secret_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the secret",
},
"parent_path": {
Type: schema.TypeString,
Optional: true,
Description: "Path of parent folder",
},
"checkout": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to retrieve secret content",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the secret",
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the folder where the secret is located",
},
"secret_text": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "Content of the secret",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Either Text or File",
},
"default_profile_id": {
Type: schema.TypeString,
Computed: true,
Description: "Default Secret Challenge Profile (used if no conditions matched)",
},
// Workflow
"workflow_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"workflow_approver": getWorkflowApproversSchema(),
"challenge_rule": getChallengeRulesSchema(),
}
}
func dataSourceSecretRead(d *schema.ResourceData, m interface{}) error {
logger.Infof("Finding vault secret")
client := m.(*restapi.RestClient)
object := vault.NewSecret(client)
object.SecretName = d.Get("secret_name").(string)
if v, ok := d.GetOk("parent_path"); ok {
object.ParentPath = v.(string)
}
if v, ok := d.GetOk("folder_id"); ok {
object.FolderID = v.(string)
}
err := object.GetByName()
if err != nil {
return fmt.Errorf("error retrieving secret with name '%s': %s", object.SecretName, err)
}
d.SetId(object.ID)
schemamap, err := vault.GenerateSchemaMap(object)
if err != nil {
return err
}
//logger.Debugf("Generated Map: %+v", schemamap)
for k, v := range schemamap {
switch k {
case "challenge_rule":
d.Set(k, v.(map[string]interface{})["rule"])
case "workflow_approver":
d.Set(k, processBackupApproverSchema(v))
default:
d.Set(k, v)
}
}
if d.Get("checkout").(bool) {
text, err := object.CheckoutSecret()
if err != nil {
return fmt.Errorf("error checking out secret content with name '%s': %s", object.SecretName, err)
}
d.Set("secret_text", text)
}
return nil
}