-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
InfisicalClient.impl.ts
189 lines (165 loc) · 4.86 KB
/
InfisicalClient.impl.ts
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
import { HttpClient, StatusCode, UnhandledError } from '@purista/core'
import { SECRET_TYPE } from './constants.js'
import { decrypt } from './decrypt.impl.js'
import { encrypt } from './encrypt.impl.js'
import type { ClientConfig, HttpClientConfigCustom, Secret, TokenData } from './types/index.js'
/**
* The internal http client to connect to the Infisical server.
*/
export class InfisicalClient extends HttpClient<HttpClientConfigCustom> {
private serviceTokenSecret: string
private tokenData: TokenData | undefined
private projectKey: string = ''
constructor(conf: ClientConfig) {
const config = {
name: 'InfisicalClient',
defaultHeaders: {
'User-Agent': `InfisicalNodeSDK`,
'content-type': 'application/json',
...conf.defaultHeaders,
},
...conf,
}
super(config)
this.serviceTokenSecret = config.bearerToken.substring(config.bearerToken.lastIndexOf('.') + 1)
}
/**
* Encrypt the given key, value and optional comment
*/
private encryptSecret(secretKey: string, secretValue: string, secretComment = '') {
const {
ciphertext: secretKeyCiphertext,
iv: secretKeyIV,
tag: secretKeyTag,
} = encrypt({
text: secretKey,
secret: this.projectKey,
})
const {
ciphertext: secretValueCiphertext,
iv: secretValueIV,
tag: secretValueTag,
} = encrypt({
text: secretValue,
secret: this.projectKey,
})
const {
ciphertext: secretCommentCiphertext,
iv: secretCommentIV,
tag: secretCommentTag,
} = encrypt({
text: secretComment,
secret: this.projectKey,
})
return {
secretKeyCiphertext,
secretKeyIV,
secretKeyTag,
secretValueCiphertext,
secretValueIV,
secretValueTag,
secretCommentCiphertext,
secretCommentIV,
secretCommentTag,
}
}
/**
* Fetches the token data from the server for given access token
*/
async getServiceTokenData() {
this.tokenData = await this.get<TokenData>('/api/v2/service-token')
this.projectKey = decrypt({
ciphertext: this.tokenData.encryptedKey,
iv: this.tokenData.iv,
tag: this.tokenData.tag,
secret: this.serviceTokenSecret,
})
return this.tokenData
}
/**
* Get a single secret
*/
async getSecret(name: string) {
if (!this.tokenData) {
this.tokenData = await this.getServiceTokenData()
}
const environment = this.tokenData.scopes[0]?.environment
if (!environment) {
throw new UnhandledError(StatusCode.InvalidToken, 'Invalid service token - environment is missing')
}
try {
const { secret: encryptedSecret } = await this.get<{ secret: Secret }>(encodeURI(`/api/v3/secrets/${name}`), {
query: {
environment,
workspaceId: this.tokenData.workspace,
type: SECRET_TYPE,
},
})
return decrypt({
ciphertext: encryptedSecret.secretValueCiphertext,
iv: encryptedSecret.secretValueIV,
tag: encryptedSecret.secretValueTag,
secret: this.projectKey,
})
} catch (error) {
if (error instanceof UnhandledError) {
if (error.errorCode === StatusCode.NotFound) {
return undefined
}
}
throw error
}
}
/**
* Set a secret.
* It will first try to update and if the secret does not exist, it will create a new one
*/
async setSecret(name: string, value: string) {
if (!this.tokenData) {
this.tokenData = await this.getServiceTokenData()
}
const environment = this.tokenData.scopes[0]?.environment
if (!environment) {
throw new UnhandledError(StatusCode.InvalidToken, 'Invalid service token - environment is missing')
}
const payload = {
environment,
workspaceId: this.tokenData.workspace,
type: SECRET_TYPE,
...this.encryptSecret(name, value),
}
try {
await this.patch<{ secret: Secret }>(encodeURI(`/api/v3/secrets/${name}`), payload)
} catch (patchError) {
this.logger.debug({ err: patchError }, 'Secret seems to be a new one')
try {
await this.post<{ secret: Secret }>(encodeURI(`/api/v3/secrets/${name}`), payload)
} catch (error) {
const err = UnhandledError.fromError(error)
this.logger.error({ err })
throw err
}
}
}
/**
* Remove a secret
*/
async removeSecret(name: string) {
if (!this.tokenData) {
this.tokenData = await this.getServiceTokenData()
}
const environment = this.tokenData.scopes[0]?.environment
if (!environment) {
throw new UnhandledError(StatusCode.InvalidToken, 'Invalid service token - environment is missing')
}
await this.delete(
encodeURI(`/api/v3/secrets/${name}`),
{},
{
environment,
workspaceId: this.tokenData.workspace,
type: SECRET_TYPE,
},
)
}
}