Skip to content

Commit

Permalink
SK-1621: add log support
Browse files Browse the repository at this point in the history
  • Loading branch information
“amith-skyflow” committed Oct 25, 2024
1 parent fb3bf16 commit 9b62b14
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 52 deletions.
81 changes: 81 additions & 0 deletions samples/vault-api/credentials-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { DeleteRequest, Env, LogLevel, Skyflow } from "skyflow-node";

//NOTE : If you don't specify credentials at config level credentials or skyflow credentials, SDK will check SKYFLOW_CREDENTIALS key in your env
// To generate Bearer Token from credentials string.
const cred = {
clientID: '<YOUR_CLIENT_ID>',
clientName: '<YOUR_CLIENT_NAME>',
keyID: '<YOUR_KEY_ID>',
tokenURI: '<YOUR_TOKEN_URI>',
privateKey: '<YOUR_PEM_PRIVATE_KEY>',
};

// please pass one of apiKey, token, credentialsString & path
const skyflowCredentials = {
credentialsString: JSON.stringify(cred)
}

const credentials = {
token: "BEARER_TOKEN", // bearer token
// apiKey: "API_KEY", //API_KEY
// path: "PATH", //PATH to credentials json
// credentialsString: "CREDENTIAL_STRING", // credentials as string
}

const skyflow_client = new Skyflow({
vaultConfigs: [
{
vaultId: "VAULT_ID1", // primary vault
clusterId: "CLUSTER_ID1", // ID from your vault URL Eg https://{clusterId}.vault.skyflowapis.com
env: Env.PROD, // Env by deault it is set to PROD
},
{
vaultId: "VAULT_ID2", // primary vault
clusterId: "CLUSTER_ID2", // ID from your vault URL Eg https://{clusterId}.vault.skyflowapis.com
env: Env.PROD, // Env by deault it is set to PROD
credentials: credentials,
}
],
skyflowCredentials: skyflowCredentials, // skyflow credentials will be used if no individual creds are passed
logLevel:LogLevel.ERROR // set loglevel by deault it is set to PROD
});

const primaryDeleteIds = [
'SKYFLOW_ID1',
'SKYFLOW_ID2',
'SKYFLOW_ID3',
]

const primaryDeleteRequest = new DeleteRequest(
"TABLE_NAME1", // TABLE_NAME
primaryDeleteIds
);

// VAULT_ID1 will use skyflowCredentials if you don't specify individual credentials at config level
skyflow_client.vault("VAULT_ID1").delete(
primaryDeleteRequest
).then(resp=>{
console.log(resp);
}).catch(err=>{
console.log(JSON.stringify(err));
});

const secondaryDeleteIds = [
'SKYFLOW_ID4',
'SKYFLOW_ID5',
'SKYFLOW_ID6',
]

const secondaryDeleteRequest = new DeleteRequest(
"TABLE_NAME2", // TABLE_NAME
secondaryDeleteIds
);

// VAULT_ID1 will use individual credentials at config level
skyflow_client.vault("VAULT_ID2").delete(
secondaryDeleteRequest
).then(resp=>{
console.log(resp);
}).catch(err=>{
console.log(JSON.stringify(err));
});
6 changes: 3 additions & 3 deletions samples/vault-api/data-residency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const skyflow_client = new Skyflow({
logLevel:LogLevel.ERROR // set loglevel by deault it is set to PROD
});

//add vault config from Sandbox env
//add vault config from prod env
skyflow_client.addVaultConfig(
{
vaultId: "VAULT_ID2", // secondary vault
clusterId: "CLUSTER_ID2", // ID from your vault URL Eg https://{clusterId}.vault.skyflowapis.com
env: Env.SANDBOX, // Env by deault it is set to PROD
env: Env.PROD, // Env by deault it is set to PROD
// if you dont specify individual creds, skyflow creds will be used
}
);
Expand All @@ -61,7 +61,7 @@ skyflow_client.vault("VALUT_ID1").get(getRequest)
console.log(getResponse.data);
const insertData = getResponse.data;
//parse insertData to remove skyflow_id
//get data from prod vault and insert data to SANDBOX vault
//get data from one vault and insert data to another vault
const insertRequest = new InsertRequest(
"TABLE_NAME",
insertData!,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const insertData = [

const insertReq = new InsertRequest(
"TABLE_NAME",
insertData
insertData,
)

const insertOptions = new InsertOptions()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 10 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,21 @@ export async function getBearerToken(credentials?: Credentials, logLevel?: LogLe
}
// If credentials are missing but environment variable exists, use it
if (!credentials && process.env.SKYFLOW_CREDENTIALS) {
printLog(logs.infoLogs.USING_SKYFLOW_CREDENTIALS_ENV, MessageType.LOG, logLevel);
credentials = {
credentialsString: process.env.SKYFLOW_CREDENTIALS
}
}

// If token already exists, resolve immediately
if (credentials?.apiKey && credentials.apiKey.trim().length > 0) {
printLog(logs.infoLogs.USING_API_KEY, MessageType.LOG, logLevel);
return { type: AuthType.API_KEY, key: credentials.apiKey };
}

// If token already exists, resolve immediately
if (credentials?.token) {
printLog(logs.infoLogs.USING_BEARER_TOKEN, MessageType.LOG, logLevel);
return { type: AuthType.TOKEN, key: validateToken(credentials.token) };

Check warning on line 195 in src/utils/index.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/index.ts#L194-L195

Added lines #L194 - L195 were not covered by tests
}

Expand All @@ -197,6 +200,8 @@ export async function getBearerToken(credentials?: Credentials, logLevel?: LogLe
// Generate token based on provided credentials
const token = await getToken(credentials, logLevel);

printLog(logs.infoLogs.BEARER_TOKEN_RESOLVED, MessageType.LOG, logLevel);

return { type: AuthType.TOKEN, key: token.accessToken };

} catch (err) {
Expand Down Expand Up @@ -255,18 +260,19 @@ export const printLog = (message: string, messageType: MessageType, logLevel: Lo
const {
showDebugLogs, showInfoLogs, showWarnLogs, showErrorLogs,
} = LogLevelOptions[logLevel];
const version = sdkDetails?.version ? `v${sdkDetails?.version}` : '';
if (messageType === MessageType.LOG && showDebugLogs) {
// eslint-disable-next-line no-console
console.log("DEBUG: [Skyflow] " + message);
console.log(`DEBUG: [Skyflow Node SDK ${version}] ` + message);
} else if (messageType === MessageType.LOG && showInfoLogs) {
// eslint-disable-next-line no-console
console.log("INFO: [Skyflow] " + message);
console.log(`INFO: [Skyflow Node SDK ${version}] ` + message);
} else if (messageType === MessageType.WARN && showWarnLogs) {
// eslint-disable-next-line no-console
console.warn("WARN: [Skyflow] " + message);
console.warn(`WARN: [Skyflow Node SDK ${version}] ` + message);
} else if (messageType === MessageType.ERROR && showErrorLogs) {
// eslint-disable-next-line no-console
console.error("ERROR: [Skyflow] " + message);
console.error(`ERROR: [Skyflow Node SDK ${version}] ` + message);
}
};

Expand Down
29 changes: 23 additions & 6 deletions src/utils/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const logs = {
GENERATE_BEARER_TOKEN_SUCCESS: "BearerToken is generated",
GENERATE_SIGNED_DATA_TOKEN_SUCCESS: 'Signed Data tokens are generated',
INITIALIZE_CLIENT: 'Initializing skyflow client.',
VALIDATING_VAULT_CONFIG: 'Validating vault config.',
VALIDATING_CONNECTION_CONFIG: 'Validating connection config.',
VAULT_CONTROLLER_INITIALIZED: 'Initialized vault controller with vault ID %s1.',
CONNECTION_CONTROLLER_INITIALIZED: 'Intitialized connection controller with connection ID %s1.',
VAULT_ID_CONFIG_EXISTS: 'Vault config with vault ID %s1 already exists.',
VAULT_ID_CONFIG_DOES_NOT_EXIST: `Vault config with vault ID %s1 doesn't exist.`,
CONNECTION_ID_CONFIG_EXISTS: `Connection config with connection ID %s1 already exists.`,
CONNECTION_ID_CONFIG_DOES_NOT_EXIST: `Connection config with connection ID %s1 doesn't exist.`,
CURRENT_LOG_LEVEL: 'Current log level is %s1.',
CLIENT_INITIALIZED: 'Initialized skyflow client successfully.',
VALIDATE_INSERT_INPUT: 'Validating insert input.',
VALIDATE_DETOKENIZE_INPUT: 'Validating detokenize input.',
Expand All @@ -16,12 +25,18 @@ const logs = {
VALIDATE_DELETE_INPUT: 'Validating delete method input.',
VALIDATE_UPDATE_INPUT: 'Validating update method input.',
VALIDATE_CONNECTION_CONFIG: 'Validating connection config.',
INSERT_DATA_SUCCESS: 'Data has been inserted successfully.',
DETOKENIZE_SUCCESS: 'Data has been revealed successfully.',
GET_BY_ID_SUCCESS: 'Data has been revealed successfully.',
INSERT_DATA_SUCCESS: 'Data inserted.',
FILE_UPLOAD_DATA_SUCCESS: 'File uploaded.',
DETOKENIZE_SUCCESS: 'Data detokenized.',
GET_SUCCESS: 'Data revealed.',
UPDATE_SUCCESS: 'Data updated.',
DELETE_SUCCESS: 'Data deleted.',
TOKENIZE_SUCCESS: 'Data tokenized.',
QUERY_SUCCESS: 'Query executed.',
BEARER_TOKEN_LISTENER: 'Get bearer token listener added.',
BEARER_TOKEN_RESOLVED: 'GetBearerToken promise resolved successfully.',
REUSE_BEARER_TOKEN: 'Reusing the bearer token.',
REUSE_BEARER_TOKEN: 'Reusing bearer token.',
REUSE_API_KEY: 'Reusing api key.',
CONTROLLER_INITIALIZED: 'SkyflowController initialized.',
INSERT_TRIGGERED: 'Insert method triggered.',
DETOKENIZE_TRIGGERED: 'Detokenize method triggered.',
Expand All @@ -44,8 +59,10 @@ const logs = {
GENERATE_SIGNED_DATA_TOKENS_TRIGGERED: "generateSignedDataTokens is triggered",
UPDATE_TRIGGERED: 'Update method triggered.',
UPDATE_REQUEST_RESOLVED: 'Update request is resolved.',
UNABLE_TO_GENERATE_SDK_METRIC: 'Unable to generate %s1 metric.'

UNABLE_TO_GENERATE_SDK_METRIC: 'Unable to generate %s1 metric.',
USING_BEARER_TOKEN: 'Using token from credentials',
USING_API_KEY: 'Using api key from credentials',
USING_SKYFLOW_CREDENTIALS_ENV: 'Using SKYFLOW_CREDENTIALS from env'
},
errorLogs: {

Expand Down
4 changes: 4 additions & 0 deletions src/vault/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SkyflowError from "../../error";
import errorMessages from "../../error/messages";
import { AuthInfo, AuthType, LogLevel, MessageType, printLog, TYPES } from "../../utils/index";
import { isExpired } from "../../utils/jwt-utils";
import logs from "../../utils/logs";
import Credentials from "../config/credentials";

class VaultClient {
Expand Down Expand Up @@ -84,8 +85,10 @@ class VaultClient {
if (this.authInfo?.key && !this.updateTriggered) {
switch (this.authInfo.type) {
case AuthType.API_KEY:
printLog(logs.infoLogs.REUSE_API_KEY, MessageType.LOG, this.logLevel);
return { apiKey: this.authInfo.key } as Credentials;
case AuthType.TOKEN:
printLog(logs.infoLogs.REUSE_BEARER_TOKEN, MessageType.LOG, this.logLevel);
if (!isExpired(this.authInfo.key)) {
return { token: this.authInfo.key } as Credentials;
}
Expand Down Expand Up @@ -154,6 +157,7 @@ class VaultClient {
grpcCode?: number,
details?: any
) {
printLog(description, MessageType.ERROR, this.getLogLevel());
reject(new SkyflowError({
http_code: err?.response?.status || 400,
message: description,
Expand Down
Loading

0 comments on commit 9b62b14

Please sign in to comment.