Skip to content

Commit

Permalink
Update packages. Support for private docker registry
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <[email protected]>

Pass headers in more places

Signed-off-by: Prabhu Subramanian <[email protected]>

Handle empty authentication tokens better

Signed-off-by: Prabhu Subramanian <[email protected]>
  • Loading branch information
prabhu committed Nov 19, 2023
1 parent c4ad255 commit d172937
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 39 deletions.
2 changes: 1 addition & 1 deletion analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const setFileRef = (allImports, src, file, pathnode, specifiers = []) => {
}
const fileRelativeLoc = relative(src, file);
// remove unexpected extension imports
if (/\.(svg|png|jpg|d\.ts)/.test(pathway)) {
if (/\.(svg|png|jpg|json|d\.ts)/.test(pathway)) {
return;
}
const importedModules = specifiers
Expand Down
71 changes: 69 additions & 2 deletions docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,78 @@ export const getOnlyDirs = (srcpath, dirName) => {
};

const getDefaultOptions = () => {
let authTokenSet = false;
const opts = {
enableUnixSockets: true,
throwHttpErrors: true,
method: "GET",
hooks: { beforeError: [] },
mutableDefaults: true
};
// Support for private registry
if (existsSync(join(homedir(), ".docker", "config.json"))) {
const configData = readFileSync(
join(homedir(), ".docker", "config.json"),
"utf-8"
);
if (configData) {
try {
const configJson = JSON.parse(configData);
if (configJson.auths) {
for (const serverAddress of Object.keys(configJson.auths)) {
if (
process.env.DOCKER_SERVER_ADDRESS &&
process.env.DOCKER_SERVER_ADDRESS.trim().length &&
process.env.DOCKER_SERVER_ADDRESS !== serverAddress
) {
continue;
}
if (configJson.auths[serverAddress].auth) {
opts.headers = {
"X-Registry-Auth": configJson.auths[serverAddress].auth
};
console.log(
`Using the existing authentication token for the docker server ${serverAddress}`
);
authTokenSet = true;
break;
}
}
}
} catch (err) {
// pass
}
}
}
if (!authTokenSet && process.env.DOCKER_AUTH_CONFIG) {
opts.headers = {
"X-Registry-Auth": process.env.DOCKER_AUTH_CONFIG
};
authTokenSet = true;
}
if (
!authTokenSet &&
process.env.DOCKER_USER &&
process.env.DOCKER_PASSWORD &&
process.env.DOCKER_EMAIL &&
process.env.DOCKER_SERVER_ADDRESS
) {
const authPayload = {
username: process.env.DOCKER_USER,
email: process.env.DOCKER_EMAIL,
serveraddress: process.env.DOCKER_SERVER_ADDRESS
};
if (process.env.DOCKER_USER === "<token>") {
authPayload.IdentityToken = process.env.DOCKER_PASSWORD;
} else {
authPayload.password = process.env.DOCKER_PASSWORD;
}
opts.headers = {
"X-Registry-Auth": Buffer.from(JSON.stringify(authPayload)).toString(
"base64"
)
};
}
const userInfo = _userInfo();
opts.podmanPrefixUrl = isWin ? "" : `http://unix:/run/podman/podman.sock:`;
opts.podmanRootlessPrefixUrl = isWin
Expand Down Expand Up @@ -163,7 +228,8 @@ export const getConnection = async (options) => {
enableUnixSockets: defaultOptions.enableUnixSockets,
throwHttpErrors: defaultOptions.throwHttpErrors,
method: defaultOptions.method,
prefixUrl: defaultOptions.prefixUrl
prefixUrl: defaultOptions.prefixUrl,
headers: defaultOptions.headers
},
options
);
Expand Down Expand Up @@ -265,7 +331,8 @@ export const makeRequest = async (path, method = "GET") => {
enableUnixSockets: defaultOptions.enableUnixSockets,
throwHttpErrors: defaultOptions.throwHttpErrors,
method: defaultOptions.method,
prefixUrl: defaultOptions.prefixUrl
prefixUrl: defaultOptions.prefixUrl,
headers: defaultOptions.headers
},
extraOptions
);
Expand Down
Loading

0 comments on commit d172937

Please sign in to comment.