Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates handling of git with self-signed cert #520

Merged
merged 2 commits into from
Oct 3, 2022
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"typescript": "^4.5.2"
},
"dependencies": {
"@cloudnativetoolkit/git-client": "^1.14.6",
"@cloudnativetoolkit/git-client": "^1.14.7",
"@cloudnativetoolkit/kubernetes-client": "^9.1.3",
"chalk": "^4.1.2",
"dot-properties": "^1.0.1",
Expand Down
6 changes: 6 additions & 0 deletions src/commands/gitops-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export const builder = (yargs: Argv<any>) => {
conflicts: 'token',
demandOption: false,
},
'username': {
describe: 'Git username to access gitops repo',
type: 'string',
conflicts: 'gitopsCredentialsFile',
demandOption: false,
},
'token': {
describe: 'Git personal access token to access gitops repo',
type: 'string',
Expand Down
6 changes: 6 additions & 0 deletions src/commands/gitops-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export const builder = (yargs: Argv<any>) => {
conflicts: 'token',
demandOption: false,
},
'username': {
describe: 'Git username to access gitops repo',
type: 'string',
conflicts: 'gitopsCredentialsFile',
demandOption: false,
},
'token': {
describe: 'Git personal access token to access gitops repo',
type: 'string',
Expand Down
21 changes: 15 additions & 6 deletions src/services/gitops-module/gitops-module-pr.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export class GitopsModulePRImpl implements GitOpsModuleApi {

async populate(options: GitOpsModuleOptions): Promise<GitOpsModuleResult> {

this.logger.log(`Populating gitops repo for component ${options.name} in namespace ${options.namespace}`);
if (options.isNamespace) {
this.logger.log(`Populating gitops repo for namespace ${options.name}`);
} else {
this.logger.log(`Populating gitops repo for component ${options.name} in namespace ${options.namespace}`);
}

const input: GitOpsModuleInput = await this.defaultInputs(options);

Expand Down Expand Up @@ -201,7 +205,7 @@ export class GitopsModulePRImpl implements GitOpsModuleApi {
return result;
}

async loadGitOpsConfig({bootstrapRepoUrl, gitopsConfigFile, token, branch, gitopsCredentials}: {bootstrapRepoUrl?: string, gitopsConfigFile?: string, branch?: string, token?: string, gitopsCredentials: GitOpsCredentials}): Promise<GitOpsConfig> {
async loadGitOpsConfig({bootstrapRepoUrl, gitopsConfigFile, caCert, branch, gitopsCredentials}: {bootstrapRepoUrl?: string, gitopsConfigFile?: string, branch?: string, caCert?: string | {cert: string, certFile: string}, gitopsCredentials: GitOpsCredentials}): Promise<GitOpsConfig> {
if (!gitopsConfigFile && !bootstrapRepoUrl && !process.env.GITOPS_CONFIG) {
throw new Error('Missing gitops config file name, bootstrap repo location, or GITOPS_CONFIG env variable');
}
Expand All @@ -213,7 +217,7 @@ export class GitopsModulePRImpl implements GitOpsModuleApi {
} else {
const credential: GitOpsCredential = this.lookupGitCredential(gitopsCredentials, bootstrapRepoUrl);

return await parseGitFile(bootstrapRepoUrl, 'config.yaml', {username: credential.username, password: credential.token}, branch) as GitOpsConfig;
return await parseGitFile(bootstrapRepoUrl, 'config.yaml', {username: credential.username, password: credential.token, caCert}, branch) as GitOpsConfig;
}
}

Expand Down Expand Up @@ -545,7 +549,7 @@ async function parseFile(filename: string): Promise<object> {
return parser(await fs.readFile(filename));
}

async function parseGitFile(gitUrl: string, filename: string, credentials: {username: string, password: string}, branch?: string): Promise<object> {
async function parseGitFile(gitUrl: string, filename: string, credentials: {username: string, password: string, caCert?: string | {cert: string, certFile: string}}, branch?: string): Promise<object> {

const extension = filename.replace(/.*[.](.*)$/, '$1');

Expand All @@ -554,9 +558,14 @@ async function parseGitFile(gitUrl: string, filename: string, credentials: {user
throw new Error('Unknown extension for parsing: ' + extension);
}

const gitApi: GitApi = await apiFromUrl(gitUrl, credentials);
try {
const gitApi: GitApi = await apiFromUrl(gitUrl, credentials, branch);

return parser(await gitApi.getFileContents({path: filename}));
return parser(await gitApi.getFileContents({path: filename}));
} catch (err) {
console.log('Error getting file from git: ', {filename, gitUrl})
throw err
}
}

async function copy(sourceDir: string, destDir: string): Promise<{stdout: string | Buffer, stderr: string | Buffer}> {
Expand Down
2 changes: 2 additions & 0 deletions src/services/gitops-module/gitops-module.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ export type GitOpsModuleOptions = GitOpsModuleInputBase & Partial<GitOpsModuleIn
gitopsCredentialsFile?: string;
autoMerge?: boolean;
rateLimit?: boolean;
username?: string;
token?: string;
valueFiles?: string;
delete?: boolean;
waitForBlocked?: string;
branch?: string;
};
export type GitOpsModuleInput = GitOpsModuleInputBase & GitOpsModuleInputDefaults & {
valueFiles: string[];
Expand Down