@@ -24,14 +24,14 @@ type hvaultRemoteService struct {
2424 * api.Client
2525
2626 keyID string
27- keypath string
27+ debug bool
2828 Address string `json:"Address"`
2929 Transitkey string `json:"Transitkey"`
3030 Vaultrole string `json:"Vaultrole"`
3131 Namespace string `json:"Namespace"`
3232}
3333
34- func NewVaultClientRemoteService (configFilePath string ) (service.Service , error ) {
34+ func NewVaultClientRemoteService (configFilePath string , debug bool ) (service.Service , error ) {
3535 ctx , err := os .ReadFile (configFilePath )
3636 if err != nil {
3737 log .Fatalln ("EXIT:ctx: failed to read vault config file with error:\n " , err .Error ())
@@ -40,8 +40,14 @@ func NewVaultClientRemoteService(configFilePath string) (service.Service, error)
4040 log .Fatalln ("EXIT:keyID len: invalid keyID" )
4141 }
4242
43+ if debug {
44+ log .Println ("DEBUG:--------------------------------------------------" )
45+ log .Println ("DEBUG: verifying keyID:" , keyID )
46+ }
47+
4348 vaultService := & hvaultRemoteService {
4449 keyID : keyID ,
50+ debug : debug ,
4551 }
4652
4753 json .Unmarshal (([]byte (ctx )), & vaultService )
@@ -50,11 +56,18 @@ func NewVaultClientRemoteService(configFilePath string) (service.Service, error)
5056
5157 keypath := fmt .Sprintf ("transit/keys/%s" , vaultService .Transitkey )
5258
59+ if debug {
60+ log .Println ("DEBUG:--------------------------------------------------" )
61+ log .Println ("DEBUG: unmarshal JSON values:" , "\n -> vaultService.Address:" , vaultService .Address , "\n -> vaultService.Trasitkey:" , vaultService .Transitkey , "\n -> vaultService.Vaultrole:" , vaultService .Vaultrole , "\n -> vaultService.Namespace:" , vaultService .Namespace , "\n -> keypath:" , keypath )
62+ }
63+
5364 client , err := api .NewClient (vaultconfig )
5465 if err != nil {
55- log .Println ("--------------------------------------------------------" )
56- log .Println ("DEBUG:client: json.Unmarshal output from configFile:" , "\n vaultService.Address:" , vaultService .Address )
57- log .Println ("--------------------------------------------------------" )
66+ if debug {
67+ log .Println ("DEBUG:--------------------------------------------------" )
68+ log .Println ("DEBUG:client: json.Unmarshal output from configFile:" , "\n vaultService.Address:" , vaultService .Address )
69+ log .Println ("DEBUG:--------------------------------------------------" )
70+ }
5871 log .Fatalln ("EXIT:client: failed to initialize Vault client with error:\n " , err .Error ())
5972 }
6073
@@ -63,9 +76,11 @@ func NewVaultClientRemoteService(configFilePath string) (service.Service, error)
6376 )
6477
6578 if err != nil {
66- log .Println ("--------------------------------------------------------" )
67- log .Println ("DEBUG:k8sAuth: json.Unmarshal output from configFile:" , "\n vaultService.Vaultrole:" , vaultService .Vaultrole )
68- log .Println ("--------------------------------------------------------" )
79+ if debug {
80+ log .Println ("DEBUG:--------------------------------------------------" )
81+ log .Println ("DEBUG:k8sAuth: json.Unmarshal output from configFile:" , "\n vaultService.Vaultrole:" , vaultService .Vaultrole )
82+ log .Println ("DEBUG:--------------------------------------------------" )
83+ }
6984 log .Fatalln ("EXIT:k8sAuth: unable to initialize Kubernetes auth method with error:\n " , err .Error ())
7085 }
7186
@@ -83,14 +98,13 @@ func NewVaultClientRemoteService(configFilePath string) (service.Service, error)
8398
8499 client .SetNamespace (vaultService .Namespace )
85100
86- // //keypath := fmt.Sprintf("transit/keys/%s", vaultService.Transitkey)
87- // keypath := "transit/keys/kleidi"
88-
89101 key , err := client .Logical ().Read (keypath )
90102 if err != nil {
91- log .Println ("--------------------------------------------------------" )
92- log .Println ("DEBUG:key: keypath:" , keypath )
93- log .Println ("--------------------------------------------------------" )
103+ if debug {
104+ log .Println ("DEBUG:--------------------------------------------------" )
105+ log .Println ("DEBUG:key: keypath:" , keypath )
106+ log .Println ("DEBUG:--------------------------------------------------" )
107+ }
94108 log .Fatalln ("EXIT:key: unable to find transit key:\n " , err .Error ())
95109 }
96110
@@ -101,20 +115,22 @@ func NewVaultClientRemoteService(configFilePath string) (service.Service, error)
101115
102116func (s * hvaultRemoteService ) Encrypt (ctx context.Context , uid string , plaintext []byte ) (* service.EncryptResponse , error ) {
103117
104- // log.Println("--------------------------------------------------------------------------------------------------")
105- // log.Println("DEBUG: unencrypted payload:", string([]byte(plaintext)))
106- // log.Println("--------------------------------------------------------------------------------------------------")
118+ if s .debug {
119+ log .Println ("DEBUG:--------------------------------------------------" )
120+ log .Println ("DEBUG: unencrypted payload:" , string ([]byte (plaintext )))
121+ log .Println ("DEBUG:--------------------------------------------------" )
122+ }
107123
108- // // keypath := fmt.Sprintf("transit/keys /%s", s.Transitkey)
124+ enckeypath := fmt .Sprintf ("transit/encrypt /%s" , s .Transitkey )
109125 // keypath := "transit/encrypt/kleidi"
110126 encodepayload := map [string ]interface {}{
111127 "plaintext" : base64 .StdEncoding .EncodeToString (plaintext ),
112128 }
113129
114- encrypt , err := s .Logical ().WriteWithContext (ctx , s . keypath , encodepayload )
130+ encrypt , err := s .Logical ().WriteWithContext (ctx , enckeypath , encodepayload )
115131 if err != nil {
116132 log .Println ("--------------------------------------------------------" )
117- log .Println ("DEBUG:encrypt:" , "\n plaintext:" , string ([]byte (plaintext )), "\n keypath:" , s . keypath , "\n encodepayload:" , encodepayload )
133+ log .Println ("DEBUG:encrypt:" , "\n plaintext:" , string ([]byte (plaintext )), "\n keypath:" , enckeypath , "\n encodepayload:" , encodepayload )
118134 log .Println ("--------------------------------------------------------" )
119135 log .Fatalln ("EXIT:encrypt: with error:\n " , err .Error ())
120136 }
@@ -150,16 +166,17 @@ func (s *hvaultRemoteService) Decrypt(ctx context.Context, uid string, req *serv
150166 return nil , fmt .Errorf ("/!\\ invalid keyID" )
151167 }
152168
169+ decryptkeypath := fmt .Sprintf ("transit/decrypt/%s" , s .Transitkey )
153170 // // keypath := fmt.Sprintf("transit/keys/%s", s.Transitkey)
154171 // keypath := "transit/decrypt/kleidi"
155172 encryptedPayload := map [string ]interface {}{
156173 "ciphertext" : string ([]byte (req .Ciphertext )),
157174 }
158175
159- encryptedResponse , err := s .Logical ().WriteWithContext (ctx , s . keypath , encryptedPayload )
176+ encryptedResponse , err := s .Logical ().WriteWithContext (ctx , decryptkeypath , encryptedPayload )
160177 if err != nil {
161178 log .Println ("--------------------------------------------------------" )
162- log .Println ("DEBUG:encryptedResponse:" , "\n keypath:" , s . keypath , "\n enresult:" , encryptedPayload )
179+ log .Println ("DEBUG:encryptedResponse:" , "\n keypath:" , decryptkeypath , "\n enresult:" , encryptedPayload )
163180 log .Println ("--------------------------------------------------------" )
164181 log .Fatalln ("EXIT:encryptedResponse: with error:" , err .Error ())
165182 }
0 commit comments