mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
Revise docs for working with CRUD secrets
This commit is contained in:
@@ -2,14 +2,24 @@
|
||||
title: "Authentication"
|
||||
---
|
||||
|
||||
To authenticate requests with Infisical, you must include an API key in the `X-API-KEY` header of HTTP requests made to the platform. You can obtain an API key in User Settings > API Keys
|
||||
To authenticate requests with Infisical, you can either use an API Key or [Infisical Token](../../../getting-started/dashboard/token); certain endpoints will accept either one or both.
|
||||
- API Key: This general-purpose authentication token provides user access to most endpoints in this reference.
|
||||
- [Infisical Token](../../../getting-started/dashboard/token): This authentication token (also referred to as the service token) is scoped to a specific project and environment and used for CRUD secret operations.
|
||||
|
||||
<AccordionGroup>
|
||||
<Accordion title="API Key">
|
||||
To authenticate requests with Infisical using the API Key, you must include an API key in the `X-API-KEY` header of HTTP requests made to the platform.
|
||||
|
||||
You can obtain an API key in User Settings > API Keys
|
||||
|
||||

|
||||

|
||||

|
||||
</Accordion>
|
||||
<Accordion title="Infisical Token">
|
||||
To authenticate requests with Infisical using the Infisical Token, you must include your Infisical Token in the `Authorization` header of HTTP requests made to the platform with the value `Bearer st.<rest_of_your_infisical_token>`.
|
||||
|
||||
<Info>
|
||||
It's important to keep your API key secure, as it grants access to your
|
||||
secrets in Infisical. For added security, set a reasonable expiration time and
|
||||
rotate your API key on a regular basis.
|
||||
</Info>
|
||||
You can obtain an Infisical Token in Project Settings > Service Tokens.
|
||||
|
||||

|
||||
</Accordion>
|
||||
</AccordionGroup>
|
||||
@@ -2,48 +2,47 @@
|
||||
title: "Create secrets"
|
||||
---
|
||||
|
||||
In this example, we demonstrate how to add secrets to a project and environment.
|
||||
In this example, we demonstrate how to add secrets to a project and environment using an Infisical Token.
|
||||
|
||||
Prerequisites:
|
||||
|
||||
- Set up and add envars to [Infisical Cloud](https://app.infisical.com)
|
||||
- Create an [Infisical Token](../../../getting-started/dashboard/token) for your project and environment.
|
||||
- Grasp a basic understanding of the system and its underlying cryptography [here](/api-reference/overview/introduction).
|
||||
|
||||
## Flow
|
||||
|
||||
1. [Get your (encrypted) private key](/api-reference/endpoints/users/me).
|
||||
2. Decrypt your (encrypted) private key with your password.
|
||||
3. [Get the (encrypted) project key for the project.](/api-reference/endpoints/workspaces/workspace-key)
|
||||
4. Decrypt the (encrypted) project key with your private key.
|
||||
5. Encrypt your secret(s) with the project key.
|
||||
6. [Send (encrypted) secret(s) to the Infical API](/api-reference/endpoints/secrets/create)
|
||||
1. [Get your Infisical Token data](/api-reference/endpoints/service-tokens/get) including a (encrypted) project key.
|
||||
2. Decrypt the (encrypted) project key with the key from your Infisical Token.
|
||||
3. Encrypt your secret(s) with the project key
|
||||
4. [Send (encrypted) secret(s) to Infical](/api-reference/endpoints/secrets/create)
|
||||
|
||||
## Example
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Javascript">
|
||||
```js
|
||||
const crypto = require('crypto');
|
||||
const axios = require('axios');
|
||||
|
||||
const BASE_URL = 'https://app.infisical.com';
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
const BLOCK_SIZE_BYTES = 16;
|
||||
|
||||
const encrypt = (
|
||||
text,
|
||||
secret
|
||||
) => {
|
||||
const iv = crypto.randomBytes(BLOCK_SIZE_BYTES);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, secret, iv);
|
||||
const encrypt = ({ text, secret }) => {
|
||||
const iv = crypto.randomBytes(BLOCK_SIZE_BYTES);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, secret, iv);
|
||||
|
||||
let ciphertext = cipher.update(text, 'utf8', 'base64');
|
||||
ciphertext += cipher.final('base64');
|
||||
return {
|
||||
ciphertext,
|
||||
iv: iv.toString('base64'),
|
||||
tag: cipher.getAuthTag().toString('base64')
|
||||
};
|
||||
let ciphertext = cipher.update(text, 'utf8', 'base64');
|
||||
ciphertext += cipher.final('base64');
|
||||
return {
|
||||
ciphertext,
|
||||
iv: iv.toString('base64'),
|
||||
tag: cipher.getAuthTag().toString('base64')
|
||||
};
|
||||
}
|
||||
|
||||
const decrypt = (ciphertext, iv, tag, secret) => {
|
||||
const decrypt = ({ ciphertext, iv, tag, secret}) => {
|
||||
const decipher = crypto.createDecipheriv(
|
||||
ALGORITHM,
|
||||
secret,
|
||||
@@ -58,95 +57,96 @@ const decrypt = (ciphertext, iv, tag, secret) => {
|
||||
}
|
||||
|
||||
const createSecrets = async () => {
|
||||
const API_KEY = 'your_api_key';
|
||||
const PSWD = 'your_pswd';
|
||||
const WORKSPACE_ID = 'your_workspace_id';
|
||||
const serviceToken = '';
|
||||
const serviceTokenSecret = serviceToken.substring(serviceToken.lastIndexOf('.') + 1);
|
||||
|
||||
const secretType = 'shared'; // 'shared' or 'personal'
|
||||
const secretKey = 'some_key';
|
||||
const secretValue = 'some_value';
|
||||
const secretComment = 'some_comment';
|
||||
|
||||
const SECRET_KEY = 'SOME_KEY';
|
||||
const SECRET_VALUE = 'SOME_VALUE';
|
||||
// 1. Get your Infisical Token data
|
||||
const { data: serviceTokenData } = await axios.get(
|
||||
`${BASE_URL}/api/v2/service-token`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 1. get (encrypted) private key
|
||||
const user = await axios.get(
|
||||
'https://api.infisical.com/api/v2/users/me', {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
// 2. Decrypt the (encrypted) project key with the key from your Infisical Token
|
||||
const projectKey = decrypt({
|
||||
ciphertext: serviceTokenData.encryptedKey,
|
||||
iv: serviceTokenData.iv,
|
||||
tag: serviceTokenData.tag,
|
||||
secret: serviceTokenSecret
|
||||
});
|
||||
|
||||
// 3. Encrypt your secret(s) with the project key
|
||||
const {
|
||||
ciphertext: secretKeyCiphertext,
|
||||
iv: secretKeyIV,
|
||||
tag: secretKeyTag
|
||||
} = encrypt({
|
||||
text: secretKey,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
// 2. decrypt your (encrypted) private key with your password
|
||||
const privateKey = decrypt({
|
||||
ciphertext: user.encryptedPrivateKey,
|
||||
iv: user.iv,
|
||||
tag: user.tag,
|
||||
secret: PSWD.slice(0, 32).padStart(32, '0');
|
||||
});
|
||||
const {
|
||||
ciphertext: secretValueCiphertext,
|
||||
iv: secretValueIV,
|
||||
tag: secretValueTag
|
||||
} = encrypt({
|
||||
text: secretValue,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
// 3. get the (encrypted) project key for the project
|
||||
const encryptedProjectKey = await axios.get(
|
||||
`https://api.infisical.com/api/v2/workspace/${WORKSPACE_ID}`, {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
const {
|
||||
ciphertext: secretCommentCiphertext,
|
||||
iv: secretCommentIV,
|
||||
tag: secretCommentTag
|
||||
} = encrypt({
|
||||
text: secretComment,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
const secret = {
|
||||
type: secretType,
|
||||
secretKeyCiphertext,
|
||||
secretKeyIV,
|
||||
secretKeyTag,
|
||||
secretValueCiphertext,
|
||||
secretValueIV,
|
||||
secretValueTag,
|
||||
secretCommentCiphertext,
|
||||
secretCommentIV,
|
||||
secretCommentTag
|
||||
}
|
||||
|
||||
// 4. decrypt the project key with your private key
|
||||
const projectKey = nacl.box.open(
|
||||
util.decodeBase64(encryptedProjectKey),
|
||||
util.decodeBase64(projectKey.nonce),
|
||||
util.decodeBase64(projectKey.sender.publicKey),
|
||||
util.decodeBase64(privateKey)
|
||||
);
|
||||
|
||||
// 5. encrypt your secret(s) with the project key
|
||||
const {
|
||||
ciphertext: secretKeyCiphertext,
|
||||
iv: secretKeyIV,
|
||||
tag: secretKeyTag
|
||||
} = encrypt(SECRET_KEY, projectKey);
|
||||
|
||||
const {
|
||||
ciphertext: secretValueCiphertext,
|
||||
iv: secretValueIV,
|
||||
tag: secretValueTag
|
||||
} = encrypt(SECRET_VALUE, projectKey);
|
||||
|
||||
const secret = {
|
||||
secretKeyCiphertext,
|
||||
secretKeyIV,
|
||||
secretKeyTag,
|
||||
secretValueCiphertext,
|
||||
secretValueIV,
|
||||
secretValueTag
|
||||
}
|
||||
|
||||
// 6. Send (encrypted) secret(s) to the Infisical API
|
||||
await axios.post(
|
||||
`https://api.infisical.com/api/v2/secrets`,
|
||||
{
|
||||
workspaceId: WORKSPACE_ID,
|
||||
environment: 'dev',
|
||||
secrets: secret
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
// 4. Send (encrypted) secret(s) to Infisical
|
||||
await axios.post(
|
||||
`${BASE_URL}/api/v2/secrets`,
|
||||
{
|
||||
workspaceId: serviceTokenData.workspace,
|
||||
environment: serviceTokenData.environment,
|
||||
secrets: [secret]
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
createSecrets();
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
<Info>
|
||||
This example uses [TweetNaCl.js](https://tweetnacl.js.org/#/), a port of
|
||||
TweetNacl/Nacl, to perform asymmeric decryption of the project key but there
|
||||
are ports of NaCl available in every major language.
|
||||
</Info>
|
||||
<Tip>
|
||||
It can be useful to perform steps 1-4 ahead of time and store away your
|
||||
private key (and even project key) for later use. The Infisical CLI works by
|
||||
securely storing your private key via your OS keyring.
|
||||
</Tip>
|
||||
</Info>
|
||||
@@ -7,24 +7,32 @@ In this example, we demonstrate how to delete secrets
|
||||
Prerequisites:
|
||||
|
||||
- Set up and add envars to [Infisical Cloud](https://app.infisical.com)
|
||||
- Create either an [API Key](/api-reference/overview/authentication) or [Infisical Token](../../../getting-started/dashboard/token) for your project and environment.
|
||||
- Grasp a basic understanding of the system and its underlying cryptography [here](/api-reference/overview/introduction).
|
||||
|
||||
## Example
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Javascript">
|
||||
</Tab>
|
||||
</Tabs>
|
||||
```js
|
||||
const axios = require('axios');
|
||||
const BASE_URL = 'https://app.infisical.com';
|
||||
|
||||
const deleteSecrets = async () => {
|
||||
const API_KEY = "your_api_key";
|
||||
const SECRET_ID = "ID"; // ID of secret to delete
|
||||
const serviceToken = 'your_service_token';
|
||||
const secretId = 'id_of_secret_to_delete';
|
||||
|
||||
// 6. Send ID(s) of secret(s) to delete to the Infisical API
|
||||
await axios.delete(
|
||||
`https://api.infisical.com/api/v2/secrets`,
|
||||
`${BASE_URL}/api/v2/secrets`,
|
||||
{
|
||||
secretIds: SECRET_ID,
|
||||
secretIds: [secretId],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"X-API-KEY": API_KEY,
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
},
|
||||
}
|
||||
);
|
||||
@@ -32,3 +40,8 @@ const deleteSecrets = async () => {
|
||||
|
||||
deleteSecrets();
|
||||
```
|
||||
|
||||
<Info>
|
||||
If using an `API_KEY` to authenticate with the Infisical API, then you should include it in the `X_API_KEY` header.
|
||||
</Info>
|
||||
|
||||
|
||||
@@ -2,48 +2,33 @@
|
||||
title: "Retrieve secrets"
|
||||
---
|
||||
|
||||
In this example, we demonstrate how to retrieve secrets from a project and environment.
|
||||
In this example, we demonstrate how to retrieve secrets from a project and environment using an Infisical Token.
|
||||
|
||||
Prerequisites:
|
||||
|
||||
- Set up and add envars to [Infisical Cloud](https://app.infisical.com)
|
||||
- Set up and add envars to [Infisical Cloud](https://app.infisical.com).
|
||||
- Create an [Infisical Token](../../../getting-started/dashboard/token) for your project and environment.
|
||||
- Grasp a basic understanding of the system and its underlying cryptography [here](/api-reference/overview/introduction).
|
||||
|
||||
## Flow
|
||||
|
||||
1. [Get your (encrypted) private key.](/api-reference/endpoints/users/me)
|
||||
2. Decrypt your (encrypted) private key with your password.
|
||||
3. [Get the (encrypted) project key for the project.](/api-reference/endpoints/workspaces/workspace-key)
|
||||
4. Decrypt the (encrypted) project key with your private key.
|
||||
5. [Get secrets for a project and environment.](/api-reference/endpoints/secrets/read)
|
||||
6. Decrypt the (encrypted) secrets
|
||||
1. [Get your Infisical Token data](/api-reference/endpoints/service-tokens/get) including a (encrypted) project key.
|
||||
2. [Get secrets for your project and environment](/api-reference/endpoints/secrets/read).
|
||||
3. Decrypt the (encrypted) project key with the key from your Infisical Token.
|
||||
4. Decrypt the (encrypted) secrets
|
||||
|
||||
## Example
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Javascript">
|
||||
```js
|
||||
const crypto = require('crypto');
|
||||
const axios = require('axios');
|
||||
|
||||
const BASE_URL = 'https://app.infisical.com';
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
const BLOCK_SIZE_BYTES = 16;
|
||||
|
||||
const encrypt = (
|
||||
text,
|
||||
secret
|
||||
) => {
|
||||
const iv = crypto.randomBytes(BLOCK_SIZE_BYTES);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, secret, iv);
|
||||
|
||||
let ciphertext = cipher.update(text, 'utf8', 'base64');
|
||||
ciphertext += cipher.final('base64');
|
||||
return {
|
||||
ciphertext,
|
||||
iv: iv.toString('base64'),
|
||||
tag: cipher.getAuthTag().toString('base64')
|
||||
};
|
||||
}
|
||||
|
||||
const decrypt = (ciphertext, iv, tag, secret) => {
|
||||
const decrypt = ({ ciphertext, iv, tag, secret}) => {
|
||||
const decipher = crypto.createDecipheriv(
|
||||
ALGORITHM,
|
||||
secret,
|
||||
@@ -51,73 +36,63 @@ const decrypt = (ciphertext, iv, tag, secret) => {
|
||||
);
|
||||
decipher.setAuthTag(Buffer.from(tag, 'base64'));
|
||||
|
||||
let cleartext = decipher.update(ciphertext, 'base64', 'utf8');
|
||||
cleartext += decipher.final('utf8');
|
||||
let cleartext = decipher.update(ciphertext, 'base64', 'utf8');
|
||||
cleartext += decipher.final('utf8');
|
||||
|
||||
return cleartext;
|
||||
return cleartext;
|
||||
}
|
||||
|
||||
const retrieveSecrets = async () => {
|
||||
const API_KEY = 'your_api_key';
|
||||
const PSWD = 'your_pswd';
|
||||
const WORKSPACE_ID = 'your_workspace_id';
|
||||
const getSecrets = async () => {
|
||||
const serviceToken = 'your_service_token';
|
||||
const serviceTokenSecret = serviceToken.substring(serviceToken.lastIndexOf('.') + 1);
|
||||
|
||||
// 1. get (encrypted) private key
|
||||
const user = await axios.get(
|
||||
'https://api.infisical.com/api/v2/users/me', {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
// 1. Get your Infisical Token data
|
||||
const { data: serviceTokenData } = await axios.get(
|
||||
`${BASE_URL}/api/v2/service-token`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 2. decrypt your (encrypted) private key with your password
|
||||
const privateKey = decrypt({
|
||||
ciphertext: user.encryptedPrivateKey,
|
||||
iv: user.iv,
|
||||
tag: user.tag,
|
||||
secret: PSWD.slice(0, 32).padStart(32, '0');
|
||||
});
|
||||
// 2. Get secrets for your project and environment
|
||||
const { data } = await axios.get(
|
||||
`${BASE_URL}/api/v2/secrets?${new URLSearchParams({
|
||||
environment: serviceTokenData.environment,
|
||||
workspaceId: serviceTokenData.workspace
|
||||
})}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 3. get the (encrypted) project key for the project
|
||||
const encryptedProjectKey = await axios.get(
|
||||
`https://api.infisical.com/api/v2/workspace/${WORKSPACE_ID}`, {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
const encryptedSecrets = data.secrets;
|
||||
|
||||
// 4. decrypt the project key with your private key
|
||||
const projectKey = nacl.box.open(
|
||||
util.decodeBase64(encryptedProjectKey),
|
||||
util.decodeBase64(projectKey.nonce),
|
||||
util.decodeBase64(projectKey.sender.publicKey),
|
||||
util.decodeBase64(privateKey)
|
||||
);
|
||||
// 3. Decrypt the (encrypted) project key with the key from your Infisical Token
|
||||
const projectKey = decrypt({
|
||||
ciphertext: serviceTokenData.encryptedKey,
|
||||
iv: serviceTokenData.iv,
|
||||
tag: serviceTokenData.tag,
|
||||
secret: serviceTokenSecret
|
||||
});
|
||||
|
||||
// 5. get (encrypted) secrets for a project and environment.
|
||||
const encryptedSecrets = await axios.get(
|
||||
'https://api.infisical.com/api/v2/secrets', {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 6. decrypt the (encrypted) secrets
|
||||
const secrets = encryptedSecrets.map((encryptedSecret) => {
|
||||
// 4. Decrypt the (encrypted) secrets
|
||||
const secrets = encryptedSecrets.map((secret) => {
|
||||
const secretKey = decrypt({
|
||||
ciphertext: encryptedSecret.secretKeyCiphertext,
|
||||
iv: encryptedSecret.secretKeyIV,
|
||||
tag: encryptedSecret.secretKeyTag
|
||||
secret: projectKey
|
||||
ciphertext: secret.secretKeyCiphertext,
|
||||
iv: secret.secretKeyIV,
|
||||
tag: secret.secretKeyTag,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
const secretValue = decrypt({
|
||||
ciphertext: encryptedSecret.secretValueCiphertext,
|
||||
iv: encryptedSecret.secretValueIV,
|
||||
tag: encryptedSecret.secretValueTag
|
||||
secret: projectKey
|
||||
ciphertext: secret.secretValueCiphertext,
|
||||
iv: secret.secretValueIV,
|
||||
tag: secret.secretValueTag,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
return ({
|
||||
@@ -125,18 +100,18 @@ const retrieveSecrets = async () => {
|
||||
secretValue
|
||||
});
|
||||
});
|
||||
|
||||
console.log('secrets: ', secrets);
|
||||
}
|
||||
|
||||
retrieveSecrets();
|
||||
getSecrets();
|
||||
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
<Info>
|
||||
This example uses [TweetNaCl.js](https://tweetnacl.js.org/#/), a port of
|
||||
TweetNacl/Nacl, to perform asymmeric decryption of the project key but there
|
||||
are ports of NaCl available in every major language.
|
||||
</Info>
|
||||
<Tip>
|
||||
It can be useful to perform steps 1-4 ahead of time and store away your
|
||||
private key (and even project key) for later use. The Infisical CLI works by
|
||||
securely storing your private key via your OS keyring.
|
||||
</Tip>
|
||||
</Info>
|
||||
@@ -2,48 +2,47 @@
|
||||
title: "Update secrets"
|
||||
---
|
||||
|
||||
In this example, we demonstrate how to update secrets
|
||||
In this example, we demonstrate how to update secrets using an Infisical Token.
|
||||
|
||||
Prerequisites:
|
||||
|
||||
- Set up and add envars to [Infisical Cloud](https://app.infisical.com)
|
||||
- Create an [Infisical Token](../../../getting-started/dashboard/token) for your project and environment.
|
||||
- Grasp a basic understanding of the system and its underlying cryptography [here](/api-reference/overview/introduction).
|
||||
|
||||
## Flow
|
||||
|
||||
1. [Get your (encrypted) private key.](/api-reference/endpoints/users/me)
|
||||
2. Decrypt your (encrypted) private key with your password.
|
||||
3. [Get the (encrypted) project key for the project.](/api-reference/endpoints/workspaces/workspace-key)
|
||||
4. Decrypt the (encrypted) project key with your private key.
|
||||
5. Encrypt your secret(s) with the project key.
|
||||
6. [Send (encrypted) updated secret(s) to the Infical API.](/api-reference/endpoints/secrets/update)
|
||||
1. [Get your Infisical Token data](/api-reference/endpoints/service-tokens/get) including a (encrypted) project key.
|
||||
2. Decrypt the (encrypted) project key with the key from your Infisical Token.
|
||||
3. Encrypt your updated secret(s) with the project key
|
||||
4. [Send (encrypted) updated secret(s) to Infical](/api-reference/endpoints/secrets/update)
|
||||
|
||||
## Example
|
||||
|
||||
<Tabs>
|
||||
<Tab title="Javascript">
|
||||
```js
|
||||
const crypto = require('crypto');
|
||||
const axios = require('axios');
|
||||
|
||||
const BASE_URL = 'https://app.infisical.com';
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
const BLOCK_SIZE_BYTES = 16;
|
||||
|
||||
const encrypt = (
|
||||
text,
|
||||
secret
|
||||
) => {
|
||||
const iv = crypto.randomBytes(BLOCK_SIZE_BYTES);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, secret, iv);
|
||||
const encrypt = ({ text, secret }) => {
|
||||
const iv = crypto.randomBytes(BLOCK_SIZE_BYTES);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, secret, iv);
|
||||
|
||||
let ciphertext = cipher.update(text, 'utf8', 'base64');
|
||||
ciphertext += cipher.final('base64');
|
||||
return {
|
||||
ciphertext,
|
||||
iv: iv.toString('base64'),
|
||||
tag: cipher.getAuthTag().toString('base64')
|
||||
};
|
||||
let ciphertext = cipher.update(text, 'utf8', 'base64');
|
||||
ciphertext += cipher.final('base64');
|
||||
return {
|
||||
ciphertext,
|
||||
iv: iv.toString('base64'),
|
||||
tag: cipher.getAuthTag().toString('base64')
|
||||
};
|
||||
}
|
||||
|
||||
const decrypt = (ciphertext, iv, tag, secret) => {
|
||||
const decrypt = ({ ciphertext, iv, tag, secret}) => {
|
||||
const decipher = crypto.createDecipheriv(
|
||||
ALGORITHM,
|
||||
secret,
|
||||
@@ -58,95 +57,96 @@ const decrypt = (ciphertext, iv, tag, secret) => {
|
||||
}
|
||||
|
||||
const updateSecrets = async () => {
|
||||
const API_KEY = 'your_api_key';
|
||||
const PSWD = 'your_pswd';
|
||||
const WORKSPACE_ID = 'your_workspace_id';
|
||||
const serviceToken = 'your_service_token';
|
||||
const serviceTokenSecret = serviceToken.substring(serviceToken.lastIndexOf('.') + 1);
|
||||
|
||||
const secretId = 'id_of_secret_to_update';
|
||||
const secretKey = 'some_key';
|
||||
const secretValue = 'updated_value';
|
||||
const secretComment = 'updated_comment';
|
||||
|
||||
const SECRET_ID = 'ID' // ID of secret to update
|
||||
const SECRET_KEY = 'SOME_KEY';
|
||||
const SECRET_VALUE = 'SOME_VALUE';
|
||||
// 1. Get your Infisical Token data
|
||||
const { data: serviceTokenData } = await axios.get(
|
||||
`${BASE_URL}/api/v2/service-token`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 1. get (encrypted) private key
|
||||
const user = await axios.get(
|
||||
'https://api.infisical.com/api/v2/users/me', {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
// 2. Decrypt the (encrypted) project key with the key from your Infisical Token
|
||||
const projectKey = decrypt({
|
||||
ciphertext: serviceTokenData.encryptedKey,
|
||||
iv: serviceTokenData.iv,
|
||||
tag: serviceTokenData.tag,
|
||||
secret: serviceTokenSecret
|
||||
});
|
||||
|
||||
// 3. Encrypt your updated secret(s) with the project key
|
||||
const {
|
||||
ciphertext: secretKeyCiphertext,
|
||||
iv: secretKeyIV,
|
||||
tag: secretKeyTag
|
||||
} = encrypt({
|
||||
text: secretKey,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
// 2. decrypt your (encrypted) private key with your password
|
||||
const privateKey = decrypt({
|
||||
ciphertext: user.encryptedPrivateKey,
|
||||
iv: user.iv,
|
||||
tag: user.tag,
|
||||
secret: PSWD.slice(0, 32).padStart(32, '0');
|
||||
});
|
||||
const {
|
||||
ciphertext: secretValueCiphertext,
|
||||
iv: secretValueIV,
|
||||
tag: secretValueTag
|
||||
} = encrypt({
|
||||
text: secretValue,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
// 3. get the (encrypted) project key for the project
|
||||
const encryptedProjectKey = await axios.get(
|
||||
`https://api.infisical.com/api/v2/workspace/${WORKSPACE_ID}`, {
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 4. decrypt the project key with your private key
|
||||
const projectKey = nacl.box.open(
|
||||
util.decodeBase64(encryptedProjectKey),
|
||||
util.decodeBase64(projectKey.nonce),
|
||||
util.decodeBase64(projectKey.sender.publicKey),
|
||||
util.decodeBase64(privateKey)
|
||||
);
|
||||
|
||||
// 5. encrypt your secret(s) with the project key
|
||||
const {
|
||||
ciphertext: secretKeyCiphertext,
|
||||
iv: secretKeyIV,
|
||||
tag: secretKeyTag
|
||||
} = encrypt(SECRET_KEY, projectKey);
|
||||
|
||||
const {
|
||||
ciphertext: secretValueCiphertext,
|
||||
iv: secretValueIV,
|
||||
tag: secretValueTag
|
||||
} = encrypt(SECRET_VALUE, projectKey);
|
||||
|
||||
const secret = {
|
||||
id: SECRET_ID,
|
||||
secretKeyCiphertext,
|
||||
secretKeyIV,
|
||||
secretKeyTag,
|
||||
secretValueCiphertext,
|
||||
secretValueIV,
|
||||
secretValueTag
|
||||
}
|
||||
|
||||
// 6. Send (encrypted) secret(s) to the Infisical API
|
||||
await axios.patch(
|
||||
`https://api.infisical.com/api/v2/secrets`,
|
||||
{
|
||||
secrets: secret
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'X-API-KEY': API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
const {
|
||||
ciphertext: secretCommentCiphertext,
|
||||
iv: secretCommentIV,
|
||||
tag: secretCommentTag
|
||||
} = encrypt({
|
||||
text: secretComment,
|
||||
secret: projectKey
|
||||
});
|
||||
|
||||
const secret = {
|
||||
id: secretId,
|
||||
workspace: serviceTokenData.workspace,
|
||||
environment: serviceTokenData.environment,
|
||||
secretKeyCiphertext,
|
||||
secretKeyIV,
|
||||
secretKeyTag,
|
||||
secretValueCiphertext,
|
||||
secretValueIV,
|
||||
secretValueTag,
|
||||
secretCommentCiphertext,
|
||||
secretCommentIV,
|
||||
secretCommentTag
|
||||
}
|
||||
|
||||
// 4. Send (encrypted) updated secret(s) to Infisical
|
||||
await axios.patch(
|
||||
`${BASE_URL}/api/v2/secrets`,
|
||||
{
|
||||
secrets: [secret]
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
updateSecrets();
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
<Info>
|
||||
This example uses [TweetNaCl.js](https://tweetnacl.js.org/#/), a port of
|
||||
TweetNacl/Nacl, to perform asymmeric decryption of the project key but there
|
||||
are ports of NaCl available in every major language.
|
||||
</Info>
|
||||
<Tip>
|
||||
It can be useful to perform steps 1-4 ahead of time and store away your
|
||||
private key (and even project key) for later use. The Infisical CLI works by
|
||||
securely storing your private key via your OS keyring.
|
||||
</Tip>
|
||||
</Info>
|
||||
@@ -12,10 +12,11 @@ With the REST API, users can create, read, update, and delete secrets, as well a
|
||||
|
||||
Using Infisical's API to manage secrets requires a basic understanding of the system and its underlying cryptography detailed [here](/security/overview).
|
||||
|
||||
- Each user has a public/private key pair that is stored with the platform; private keys are encrypted locally by the user's password before being sent off to the server during the account signup process.
|
||||
- Each user has a public/private key pair that is stored with the platform; private keys are encrypted locally by protected keys that are encrypted by keys derived from Argon2id applied to the user's password before being sent off to the server during the account signup process.
|
||||
- Each (encrypted) secret belongs to a project and environment.
|
||||
- Each project has an (encrypted) project key used to encrypt the secrets within that project; Infisical stores copies of the project key, for each member of that project, encrypted under each member's public key.
|
||||
- Secrets are encrypted symmetrically by your copy of the project key belonging to the project containing.
|
||||
- Infisical Tokens contain a symmetric key that can be used to decrypt a copy of a project key from the [call to get the Infisical Token data](/api-reference/endpoints/service-tokens/get).
|
||||
- Infisical uses AES256-GCM and [TweetNaCl.js](https://tweetnacl.js.org/#/) for symmetric and asymmetric encryption/decryption operations.
|
||||
|
||||
<Info>
|
||||
|
||||
Reference in New Issue
Block a user