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
34 changes: 22 additions & 12 deletions lib/docker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import type { SecureContextOptions } from 'tls';
export class DockerClient {
private api: HTTPClient;

constructor(agent: http.Agent) {
this.api = new HTTPClient(agent);
constructor(agent: http.Agent, userAgent: string = 'docker/node-sdk') {
this.api = new HTTPClient(agent, userAgent);
}

/**
Expand All @@ -37,6 +37,7 @@ export class DockerClient {
static fromDockerHost(
dockerHost: string,
certificates?: string | SecureContextOptions,
userAgent?: string,
): Promise<DockerClient> {
return new Promise((resolve, reject) => {
if (dockerHost.startsWith('unix:')) {
Expand All @@ -47,7 +48,7 @@ export class DockerClient {
const agent = new SocketAgent(() =>
net.createConnection(socketPath),
);
resolve(new DockerClient(agent));
resolve(new DockerClient(agent, userAgent));
} catch (error) {
reject(
new Error(
Expand Down Expand Up @@ -84,7 +85,7 @@ export class DockerClient {
);
}

resolve(new DockerClient(agent));
resolve(new DockerClient(agent, userAgent));
} catch (error) {
reject(
new Error(
Expand All @@ -98,7 +99,7 @@ export class DockerClient {
const agent = new SocketAgent(
SSH.createSocketFactory(dockerHost),
);
resolve(new DockerClient(agent));
resolve(new DockerClient(agent, userAgent));
} catch (error) {
reject(
new Error(
Expand All @@ -124,6 +125,7 @@ export class DockerClient {
*/
static async fromDockerContext(
contextName?: string,
userAgent?: string,
): Promise<DockerClient> {
// Use DOCKER_CONTEXT environment variable if contextName not provided
const targetContext = contextName || process.env.DOCKER_CONTEXT;
Expand All @@ -133,12 +135,10 @@ export class DockerClient {
'No context name provided and DOCKER_CONTEXT environment variable is not set',
);
}
const contextsDir = path.join(
os.homedir(),
'.docker',
'contexts',
'meta',
);

const configDir = process.env.DOCKER_CONFIG || os.homedir();
const contextsDir = path.join(configDir, '.docker', 'contexts', 'meta');
const tlsDir = path.join(configDir, '.docker', 'contexts', 'tls');

try {
// Read all directories in the contexts meta directory
Expand Down Expand Up @@ -170,7 +170,17 @@ export class DockerClient {
meta.Endpoints.docker.Host
) {
const dockerHost = meta.Endpoints.docker.Host;
return DockerClient.fromDockerHost(dockerHost);
let certificates: string | undefined =
undefined;
const tls = path.join(tlsDir, contextDir);
if (fs.existsSync(tls)) {
certificates = tls;
}
return DockerClient.fromDockerHost(
dockerHost,
certificates,
userAgent,
);
} else {
throw new Error(
`Docker context '${targetContext}' found but has no valid Docker endpoint`,
Expand Down
6 changes: 4 additions & 2 deletions lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ export interface HTTPResponse {
*/
export class HTTPClient {
private agent: http.Agent;
private userAgent: string;

constructor(agent: http.Agent) {
constructor(agent: http.Agent, userAgent: string) {
this.agent = agent;
this.userAgent = userAgent;
}

close() {
Expand Down Expand Up @@ -122,7 +124,7 @@ export class HTTPClient {
// Prepare headers
const requestHeaders: Record<string, string> = {
Host: 'host',
'User-Agent': 'node-sdk/0.0.1',
'User-Agent': this.userAgent,
Accept: accept,
...headers,
};
Expand Down