-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
156 lines (138 loc) · 3.98 KB
/
util.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
package kmipengine
import (
"bytes"
"encoding/json"
"encoding/pem"
"fmt"
"reflect"
)
type (
tlsKeyType = string
)
const (
CABits = "tls_ca_key_bits"
CAType = "tls_ca_key_type"
CAClientType = "tls_client_key_type"
CAClientBits = "tls_client_key_bits"
CAClientTTL = "tls_client_key_ttl"
)
const (
ecKeyType tlsKeyType = "ec"
rsaKeyType tlsKeyType = "rsa"
)
type operation uint8
const (
OperationAddAttribute operation = iota
OperationCreate
OperationCreateKeypair
OperationDecrypt
OperationDeleteAttribute
OperationDestroy
OperationDiscoverVersions
OperationEncrypt
OperationGet
OperationGetAttributeList
OperationGetAttributes
OperationImport
OperationLocate
OperationMac
OperationMacVerify
OperationModifyAttribute
OperationQuery
OperationRegister
OperationRekey
OperationRekeyKeypair
OperationRevoke
OperationSign
OperationSignatureVerify
OperationRngSeed
OperationRngRetrieve
OperationAll
)
const (
errPathDataIsEmpty = "path data is empty"
errNeedForceParam = "scope not empty, need force parameter"
)
var Operations = map[operation]string{
OperationAddAttribute: "operation_add_attribute",
OperationCreate: "operation_create",
OperationCreateKeypair: "operation_create_keypair",
OperationDecrypt: "operation_decrypt",
OperationDeleteAttribute: "operation_delete_attribute",
OperationDestroy: "operation_destroy",
OperationDiscoverVersions: "operation_discover_versions",
OperationEncrypt: "operation_encrypt",
OperationGet: "operation_get",
OperationGetAttributeList: "operation_get_attribute_list",
OperationGetAttributes: "operation_get_attributes",
OperationImport: "operation_import",
OperationLocate: "operation_locate",
OperationMac: "operation_mac",
OperationMacVerify: "operation_mac_verify",
OperationModifyAttribute: "operation_modify_attribute",
OperationQuery: "operation_query",
OperationRegister: "operation_register",
OperationRekey: "operation_rekey",
OperationRekeyKeypair: "operation_rekey_keypair",
OperationRevoke: "operation_revoke",
OperationSign: "operation_sign",
OperationSignatureVerify: "operation_signature_verify",
OperationRngSeed: "operation_rng_seed",
OperationRngRetrieve: "operation_rng_retrieve",
OperationAll: "operation_all",
}
// CertPEM Convert certificate content to PEM format
func CertPEM(certBytes []byte) (string, error) {
var pemBuffer bytes.Buffer
err := pem.Encode(&pemBuffer, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})
if err != nil {
fmt.Println("Failed to encode to PEM:", err)
return "", err
}
// Save PEM format content to variable pemCert
pemCert := pemBuffer.Bytes()
return string(pemCert), nil
}
// Auxiliary function to convert map [string] interface {} into a structure
func MapToStruct(data map[string]interface{}, result interface{}) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
err = json.Unmarshal(jsonData, &result)
if err != nil {
return err
}
return nil
}
func structToMapWithJsonTags(input interface{}) (map[string]interface{}, error) {
result := make(map[string]interface{})
val := reflect.ValueOf(input)
if val.Kind() != reflect.Struct {
return nil, fmt.Errorf("input is not a struct")
}
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldName := typ.Field(i).Name
// Using JSON tags as keys in the map
jsonTag := typ.Field(i).Tag.Get("json")
if jsonTag != "" && jsonTag != "-" {
result[jsonTag] = field.Interface()
} else {
// If there is no JSON tag, use the field name as the key
result[fieldName] = field.Interface()
}
}
return result, nil
}
// PolicyRaw return policy context
func PolicyRaw(name string) string {
policy := fmt.Sprintf(policyContent, name)
return policy
}
var policyContent = `
path "/transit-%s/*"{
capabilities = ["create", "read", "update", "delete","list"]
}
`