-
Notifications
You must be signed in to change notification settings - Fork 656
/
data_source_ibm_is_bare_metal_server_initialization.go
226 lines (207 loc) · 7.74 KB
/
data_source_ibm_is_bare_metal_server_initialization.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/ScaleFT/sshkeys"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isBareMetalServerImageName = "image_name"
isBareMetalServerUserAccountUserName = "username"
isBareMetalServerUserAccountEncryptionKey = "encryption_key"
isBareMetalServerUserAccountEncPwd = "encrypted_password"
isBareMetalServerUserAccountPassword = "password"
isBareMetalServerPEM = "private_key"
isBareMetalServerPassphrase = "passphrase"
isBareMetalServerUserAccountResourceType = "resource_type"
)
func DataSourceIBMIsBareMetalServerInitialization() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISBareMetalServerInitializationRead,
Schema: map[string]*schema.Schema{
isBareMetalServerID: {
Type: schema.TypeString,
Required: true,
Description: "The bare metal server identifier",
},
isBareMetalServerPEM: {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Bare Metal Server Private Key file",
},
isBareMetalServerPassphrase: {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Passphrase for Bare Metal Server Private Key file",
},
isBareMetalServerImage: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the image the bare metal server was provisioned from",
},
isBareMetalServerImageName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined or system-provided name for the image the bare metal server was provisioned from",
},
isBareMetalServerUserAccounts: {
Type: schema.TypeList,
Computed: true,
Description: "The user accounts that are created at initialization. There can be multiple account types distinguished by the resource_type attribute.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isBareMetalServerUserAccountUserName: {
Type: schema.TypeString,
Computed: true,
Description: "The username for the account created at initialization",
},
isBareMetalServerUserAccountEncryptionKey: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for the encryption key",
},
isBareMetalServerUserAccountEncPwd: {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The password at initialization, encrypted using encryption_key, and returned base64-encoded",
},
isBareMetalServerUserAccountPassword: {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The password at initialization, encrypted using encryption_key, and returned base64-encoded",
},
isBareMetalServerUserAccountResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced : [ host_user_account ]",
},
},
},
},
isBareMetalServerKeys: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "SSH key Ids for the bare metal server",
},
},
}
}
func dataSourceIBMISBareMetalServerInitializationRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
bareMetalServerID := d.Get(isBareMetalServerID).(string)
sess, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
options := &vpcv1.GetBareMetalServerInitializationOptions{
ID: &bareMetalServerID,
}
initialization, response, err := sess.GetBareMetalServerInitializationWithContext(context, options)
if err != nil || initialization == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error getting Bare Metal Server (%s) initialization : %s\n%s", bareMetalServerID, err, response))
}
d.SetId(bareMetalServerID)
if initialization.Image != nil {
d.Set(isBareMetalServerImage, initialization.Image.ID)
d.Set(isBareMetalServerImageName, initialization.Image.Name)
}
var keys []string
keys = make([]string, 0)
if initialization.Keys != nil {
for _, key := range initialization.Keys {
keys = append(keys, *key.ID)
}
}
d.Set(isBareMetalServerKeys, flex.NewStringSet(schema.HashString, keys))
accList := make([]map[string]interface{}, 0)
if initialization.UserAccounts != nil {
for _, accIntf := range initialization.UserAccounts {
acc := accIntf.(*vpcv1.BareMetalServerInitializationUserAccount)
currAccount := map[string]interface{}{
isBareMetalServerUserAccountUserName: *acc.Username,
}
currAccount[isBareMetalServerUserAccountResourceType] = *acc.ResourceType
currAccount[isBareMetalServerUserAccountEncryptionKey] = *acc.EncryptionKey.CRN
encPassword := base64.StdEncoding.EncodeToString(*acc.EncryptedPassword)
currAccount[isBareMetalServerUserAccountEncPwd] = encPassword
var rsaKey *rsa.PrivateKey
if privatekey, ok := d.GetOk(isBareMetalServerPEM); ok {
keyFlag := privatekey.(string)
keybytes := []byte(keyFlag)
if keyFlag != "" {
block, err := pem.Decode(keybytes)
if block == nil {
return diag.FromErr(fmt.Errorf("[ERROR] Failed to load the private key from the given key contents. Instead of the key file path, please make sure the private key is pem format (%v)", err))
}
isEncrypted := false
if block.Type == "OPENSSH PRIVATE KEY" {
var err error
isEncrypted, err = isOpenSSHPrivKeyEncrypted(block.Bytes)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Failed to check if the provided open ssh key is encrypted or not %s", err))
}
} else {
isEncrypted = x509.IsEncryptedPEMBlock(block)
}
passphrase := ""
var privateKey interface{}
if isEncrypted {
if pass, ok := d.GetOk(isBareMetalServerPassphrase); ok {
passphrase = pass.(string)
} else {
return diag.FromErr(fmt.Errorf("[ERROR] Mandatory field 'passphrase' not provided"))
}
var err error
privateKey, err = sshkeys.ParseEncryptedRawPrivateKey(keybytes, []byte(passphrase))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Fail to decrypting the private key: %s", err))
}
} else {
var err error
privateKey, err = sshkeys.ParseEncryptedRawPrivateKey(keybytes, nil)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Fail to decrypting the private key: %s", err))
}
}
var ok bool
rsaKey, ok = privateKey.(*rsa.PrivateKey)
if !ok {
return diag.FromErr(fmt.Errorf("[ERROR] Failed to convert to RSA private key"))
}
}
}
if acc.EncryptedPassword != nil {
ciphertext := *acc.EncryptedPassword
password := base64.StdEncoding.EncodeToString(ciphertext)
if rsaKey != nil {
rng := rand.Reader
clearPassword, err := rsa.DecryptPKCS1v15(rng, rsaKey, ciphertext)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Can not decrypt the password with the given key, %s", err))
}
password = string(clearPassword)
}
currAccount[isBareMetalServerUserAccountPassword] = password
}
accList = append(accList, currAccount)
}
d.Set(isBareMetalServerUserAccounts, accList)
}
return nil
}