secret-manager-lig
v1.0.9
Published
Uma biblioteca para gerenciar e deixar mais segura as variáveis de ambiente usando o Google Secret Manager, inspirada na criação de uma lib feita em java pelo desenvolvedor Tulio Vasconcelos
Downloads
22
Readme
Secret Manager Lib (English)
This library helps Node.js applications securely and automatically load environment variables from Google Secret Manager. It also supports programmatically updating secrets stored in GCP.
🚀 Installation
1. Install the library:
npm install secret-manager-lig2. Install Google Cloud SDK:
For macOS:
brew install --cask google-cloud-sdkFor Windows:
Go to the installer
3. Authenticate and configure:
gcloud auth login
gcloud config set project abcinc-operation-ab-hdg-prd
gcloud auth application-default loginReplace
abcinc-operation-ab-hdg-prdwith your actual GCP project ID.
⚙️ Project Configuration
Create a .env file and add:
GCP_PROJECT_ID=your-project-id
SECRET_NAME=xx-operation-gcp-prd🛠️ How to Use in Your App
Automatically load secrets on startup:
import { initializeSecrets } from 'secret-manager-lig';
async function main() {
await initializeSecrets('your-project-id', 'your-secret-name', {
printSecrets: true, // Print secrets to console
enable: true, // Controlled via env variable
});
console.log('App started!');
}
main();Update or create secrets:
import { SecretManagerService } from './SecretManagerService';
const secretManagerService = new SecretManagerService();
async function updateSecrets() {
const projectId = process.env.GCP_PROJECT_ID || 'your-project-id';
const secretName = process.env.SECRET_NAME || 'your-secret-name';
const newVariables = {
NEW_VARIABLE: 'NEW_VALUE',
ANOTHER_VARIABLE: 'ANOTHER_VALUE',
};
await secretManagerService.updateSecretJson(projectId, secretName, newVariables);
console.log('Secrets updated successfully!');
}📌 Benefits
- No need to manually manage
.envfiles; - Secure and centralized secret management;
- Simplifies onboarding in team environments.
Compatibility
This library is compatible with:
- Node.js ≥ 16.0.0
- TypeScript ≥ 4.5.0
Node.js compatibility and Google Secret Manager dependency
Important:
- For Node.js 16.x, you must install
@google-cloud/secret-manager@^5.4.0. - For Node.js 18.x or higher, you can install
@google-cloud/secret-manager@^6.0.0or higher.
The library automatically detects your Node.js version and will warn you at runtime if you are using an incompatible version of @google-cloud/secret-manager.
Example:
# For Node 16.x
npm install @google-cloud/secret-manager@^5.x.x
# For Node 18.x or higher
npm install @google-cloud/secret-manager@^6.0.0Secret Manager Lib(BR)
Esta biblioteca facilita o uso do Google Secret Manager em aplicações Node.js. Com ela, você consegue carregar automaticamente variáveis de ambiente a partir de um segredo armazenado no Google Cloud, além de atualizá-lo sempre que necessário.
🚀 Instalação
1. Instale a biblioteca:
npm install secret-manager-lig2. Instale o SDK do Google Cloud:
Para macOS:
brew install --cask google-cloud-sdkPara Windows:
Acesse o instalador aqui
3. Autenticação e configuração
Autentique-se com o Google Cloud:
gcloud auth login
gcloud config set project abcinc-operation-ab-hdg-prd
gcloud auth application-default loginSubstitua
abcinc-operation-ab-hdg-prdpelo ID do seu projeto.
⚙️ Configuração do projeto
Adicione as variáveis no seu .env:
projectId=seu-project-id
appName=xx-operation-gcp-prd🛠️ Como usar na aplicação
Inicialização automática dos secrets:
import { initializeSecrets } from 'secret-manager-lig';
async function main() {
await initializeSecrets('seu-project-id', 'seu-secret-name', {
printSecrets: true, // Imprime os segredos no console
enable: true, // Controla via variável de ambiente
});
console.log('Aplicação iniciada!');
}
main();Atualizar ou criar segredos:
import { SecretManagerService } from './SecretManagerService';
const secretManagerService = new SecretManagerService();
async function updateSecrets() {
const projectId = process.env.GCP_PROJECT_ID || 'seu-project-id';
const secretName = process.env.SECRET_NAME || 'seu-secret-name';
const newVariables = {
NEW_VARIABLE: 'NEW_VALUE',
ANOTHER_VARIABLE: 'ANOTHER_VALUE',
};
await secretManagerService.updateSecretJson(projectId, secretName, newVariables);
console.log('Segredo atualizado com sucesso!');
}📌 Benefícios
- Elimina a necessidade de manter
.envmanual; - Garante maior segurança com os dados sensíveis;
- Ideal para projetos com múltiplos desenvolvedores.
Compatibilidade
Esta biblioteca é compatível com:
- Node.js ≥ 16.0.0
- TypeScript ≥ 4.5.0
Compatibilidade com Node.js e dependência do Google Secret Manager
Importante:
- Para Node.js 16.x, você deve instalar
@google-cloud/secret-manager@^5.4.0. - Para Node.js 18.x ou superior, você pode instalar
@google-cloud/secret-manager@^6.0.0ou superior.
A biblioteca detecta automaticamente a versão do seu Node.js e exibirá um aviso em tempo de execução se você estiver usando uma versão incompatível de @google-cloud/secret-manager.
Exemplo:
# Para Node 16.x
npm install @google-cloud/secret-manager@^5.4.0
# Para Node 18.x ou superior
npm install @google-cloud/secret-manager@^6.0.0