-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodify.go
More file actions
98 lines (83 loc) · 2.82 KB
/
Copy pathmodify.go
File metadata and controls
98 lines (83 loc) · 2.82 KB
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
// Copyright (c) 2025 Securosys SA.
// SPDX-License-Identifier: MPL-2.0
package client
import (
"bytes"
"context"
"encoding/json"
"net/http"
helpers "github.com/securosys-com/tsb-client-go/helpers"
)
// Function thats send request modify key to TSB
func (c *TSBClient) Modify(ctx context.Context, label string, password string, policy helpers.Policy) (int, error) {
if ctx == nil {
ctx = context.Background()
}
policyJson, _ := json.Marshal(policy)
policyString := string(`,"policy":` + string(policyJson))
charsPasswordJson, _ := json.Marshal(helpers.StringToCharArray(password))
passwordString := ""
if len(charsPasswordJson) > 2 {
passwordString = `"keyPassword": ` + string(charsPasswordJson) + `,`
}
var jsonStr = []byte(`{
"modifyRequest":{
` + passwordString + `
"modifyKeyName": "` + label + `"
` + policyString + `}
}`)
req, err := http.NewRequestWithContext(ctx, "POST", c.HostURL+"/v1/synchronousModify", bytes.NewBuffer(jsonStr))
if err != nil {
return 500, err
}
_, code, errRes := c.doRequest(req, KeyManagementTokenName)
if errRes != nil {
return code, errRes
}
return code, nil
}
// Function thats send asynchronous request modify key to TSB
func (c *TSBClient) AsyncModify(ctx context.Context, label string, password string, policy helpers.Policy, customMetaData map[string]string) (string, int, error) {
if ctx == nil {
ctx = context.Background()
}
var additionalMetaDataInfo map[string]string = make(map[string]string)
metaDataB64, metaDataSignature, err := c.PrepareMetaData("Modify", additionalMetaDataInfo, customMetaData)
if err != nil {
return "", 500, err
}
policyJson, _ := json.Marshal(policy)
policyString := string(`,"policy":` + string(policyJson))
charsPasswordJson, _ := json.Marshal(helpers.StringToCharArray(password))
passwordString := ""
if len(charsPasswordJson) > 2 {
passwordString = `"keyPassword": ` + string(charsPasswordJson) + `,`
}
metaDataSignatureString := "null"
if metaDataSignature != nil {
metaDataSignatureString = `"` + *metaDataSignature + `"`
}
requestJson := `{"modifyKeyName": "` + label + `",
` + passwordString + `
"metaData": "` + metaDataB64 + `",
"metaDataSignature": ` + metaDataSignatureString + `
` + policyString + `}`
var jsonStr = []byte(helpers.MinifyJson(`{
"modifyRequest":` + requestJson + `,
"requestSignature":` + string(c.GenerateRequestSignature(requestJson)) + `
}`))
req, err := http.NewRequestWithContext(ctx, "POST", c.HostURL+"/v1/modify", bytes.NewBuffer(jsonStr))
if err != nil {
return "", 500, err
}
body, code, errRes := c.doRequest(req, KeyManagementTokenName)
if errRes != nil {
return "", code, errRes
}
var result map[string]interface{}
errJSON := json.Unmarshal(body, &result)
if errJSON != nil {
return "", code, errJSON
}
return result["modifyKeyRequestId"].(string), code, nil
}