buildx: resolveCertsDriverOpts

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-10 12:08:44 +01:00
parent b223e0a42b
commit ec7bb99421
3 changed files with 150 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
* limitations under the License.
*/
import fs from 'fs';
import path from 'path';
import * as exec from '@actions/exec';
import * as semver from 'semver';
@@ -23,6 +24,8 @@ import {Context} from '../context';
import {Inputs} from './inputs';
import {Install} from './install';
import {Cert} from '../types/buildx';
export interface BuildxOpts {
context: Context;
standalone?: boolean;
@@ -126,4 +129,43 @@ export class Buildx {
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
}
public static resolveCertsDriverOpts(driver: string, endpoint: string, cert: Cert): Array<string> {
let url: URL;
try {
url = new URL(endpoint);
} catch (e) {
return [];
}
if (url.protocol != 'tcp:') {
return [];
}
const driverOpts: Array<string> = [];
if (Object.keys(cert).length == 0) {
return driverOpts;
}
let host = url.hostname;
if (url.port.length > 0) {
host += `-${url.port}`;
}
if (cert.cacert !== undefined) {
const cacertpath = path.join(Buildx.certsDir, `cacert_${host}.pem`);
fs.writeFileSync(cacertpath, cert.cacert);
driverOpts.push(`cacert=${cacertpath}`);
}
if (cert.cert !== undefined) {
const certpath = path.join(Buildx.certsDir, `cert_${host}.pem`);
fs.writeFileSync(certpath, cert.cert);
driverOpts.push(`cert=${certpath}`);
}
if (cert.key !== undefined) {
const keypath = path.join(Buildx.certsDir, `key_${host}.pem`);
fs.writeFileSync(keypath, cert.key);
driverOpts.push(`key=${keypath}`);
}
if (driver != 'remote') {
return [];
}
return driverOpts;
}
}

21
src/types/buildx.ts Normal file
View File

@@ -0,0 +1,21 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Cert {
cacert?: string;
cert?: string;
key?: string;
}