Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/onprem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function onpremCMD(namespace?: string) {
const k8s = new K8s();
const utils = new Utils();

const cfCreds = cf.getCredentials();
const cfCreds = await cf.getCredentials();

if (cfCreds && cfCreds.baseUrl === 'https://g.codefresh.io/api') {
console.error(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/pipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function pipelinesCMD(namespace?: string, runtime?: string) {
const cf = new Codefresh();
const k8s = new K8s();
const utils = new Utils();
const cfCreds = cf.getCredentials();
const cfCreds = await cf.getCredentials();

if (!namespace) {
logger.info('No namespace provided, prompting user to select one.');
Expand Down
31 changes: 29 additions & 2 deletions src/logic/codefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ export class Codefresh {
logger.info('Codefresh class instance created.');
}

getCredentials() {
async getCredentials() {
logger.info('Fetching Codefresh credentials...');
const envToken = Deno.env.get('CF_API_KEY');
const envUrl = Deno.env.get('CF_URL');
let cf_creds: CodefreshCredentials | null = null;

if (envToken && envUrl) {
logger.info('Using Codefresh credentials from environment variables.');
const formattedUrl = envUrl.endsWith('/') ? envUrl.slice(0, -1) : envUrl;
cf_creds = {
headers: { Authorization: envToken },
baseUrl: `${envUrl}/api`,
baseUrl: `${formattedUrl}/api`,
};
const isValid = await this.validateCredentials(cf_creds);
if (!isValid) {
return null;
}
return cf_creds;
}

Expand All @@ -36,17 +41,39 @@ export class Codefresh {
headers: { Authorization: currentContext.token },
baseUrl: `${currentContext.url}/api`,
};
const isValid = await this.validateCredentials(cf_creds);
if (!isValid) {
return null;
}
}

return cf_creds;
}

async validateCredentials(cfCreds: CodefreshCredentials) {
logger.info('Validating Codefresh credentials...');
const tokenID = cfCreds.headers['Authorization'].split('.')[0];
const response = await fetch(`${cfCreds.baseUrl}/auth/key/${tokenID}`, {
method: 'GET',
headers: cfCreds.headers,
});

if (!response.ok) {
logger.error(`Invalid Codefresh credentials. Status: ${response.status}`);
return false;
}
logger.info('Codefresh credentials are valid.');
return true;
}

async getAccountRuntimes(cfCreds: CodefreshCredentials) {
logger.info('Fetching account runtimes...');
const response = await fetch(`${cfCreds.baseUrl}/runtime-environments`, {
method: 'GET',
headers: cfCreds.headers,
});
const runtimes = await response.json();
console.debug(runtimes);
return runtimes;
}

Expand Down