Files
docker/ts/classes.secret.ts

130 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './plugins.js';
import { DockerHost } from './classes.host.js';
import { DockerResource } from './classes.base.js';
2019-09-12 14:45:36 +02:00
// interfaces
2022-10-17 09:36:35 +02:00
import * as interfaces from './interfaces/index.js';
2019-09-12 14:45:36 +02:00
export class DockerSecret extends DockerResource {
// STATIC (Internal - prefixed with _ to indicate internal use)
/**
* Internal: Get all secrets
* Public API: Use dockerHost.listSecrets() instead
*/
public static async _list(dockerHostArg: DockerHost) {
2019-09-12 14:45:36 +02:00
const response = await dockerHostArg.request('GET', '/secrets');
const secrets: DockerSecret[] = [];
for (const secret of response.body) {
const dockerSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(dockerSecretInstance, secret);
secrets.push(dockerSecretInstance);
}
return secrets;
}
/**
* Internal: Get secret by ID
* Public API: Use dockerHost.getSecretById(id) instead
*/
public static async _fromId(dockerHostArg: DockerHost, idArg: string) {
const secrets = await this._list(dockerHostArg);
2020-09-30 16:35:24 +00:00
return secrets.find((secret) => secret.ID === idArg);
2019-09-12 14:45:36 +02:00
}
/**
* Internal: Get secret by name
* Public API: Use dockerHost.getSecretByName(name) instead
*/
public static async _fromName(
dockerHostArg: DockerHost,
nameArg: string,
) {
const secrets = await this._list(dockerHostArg);
2020-09-30 16:35:24 +00:00
return secrets.find((secret) => secret.Spec.Name === nameArg);
2019-09-12 14:45:36 +02:00
}
/**
* Internal: Create a secret
* Public API: Use dockerHost.createSecret(descriptor) instead
*/
public static async _create(
2019-09-13 18:20:12 +02:00
dockerHostArg: DockerHost,
secretDescriptor: interfaces.ISecretCreationDescriptor,
2019-09-13 18:20:12 +02:00
) {
2019-09-13 14:40:38 +02:00
const labels: interfaces.TLabels = {
...secretDescriptor.labels,
2020-09-30 16:35:24 +00:00
version: secretDescriptor.version,
2019-09-13 14:40:38 +02:00
};
2019-09-12 14:45:36 +02:00
const response = await dockerHostArg.request('POST', '/secrets/create', {
Name: secretDescriptor.name,
2019-09-13 14:40:38 +02:00
Labels: labels,
2020-09-30 16:35:24 +00:00
Data: plugins.smartstring.base64.encode(secretDescriptor.contentArg),
2019-09-12 14:45:36 +02:00
});
2019-09-13 18:20:12 +02:00
2019-09-12 14:45:36 +02:00
const newSecretInstance = new DockerSecret(dockerHostArg);
Object.assign(newSecretInstance, response.body);
2019-09-13 18:20:12 +02:00
Object.assign(
newSecretInstance,
await DockerSecret._fromId(dockerHostArg, newSecretInstance.ID),
2019-09-13 18:20:12 +02:00
);
2019-09-12 14:45:36 +02:00
return newSecretInstance;
}
// INSTANCE PROPERTIES
2019-09-12 14:45:36 +02:00
public ID: string;
public Spec: {
Name: string;
Labels: interfaces.TLabels;
};
2019-09-13 14:40:38 +02:00
public Version: {
2019-09-13 18:20:12 +02:00
Index: string;
2019-09-13 14:40:38 +02:00
};
2019-09-12 14:45:36 +02:00
constructor(dockerHostArg: DockerHost) {
super(dockerHostArg);
2019-09-12 14:45:36 +02:00
}
// INSTANCE METHODS
2019-09-12 14:45:36 +02:00
/**
* Refreshes this secret's state from the Docker daemon
*/
public async refresh(): Promise<void> {
const updated = await DockerSecret._fromId(this.dockerHost, this.ID);
if (updated) {
Object.assign(this, updated);
}
}
/**
* Updates a secret
2019-09-12 14:45:36 +02:00
*/
2019-09-13 18:20:12 +02:00
public async update(contentArg: string) {
2019-09-12 14:45:36 +02:00
const route = `/secrets/${this.ID}/update?=version=${this.Version.Index}`;
2019-09-13 18:20:12 +02:00
const response = await this.dockerHost.request(
'POST',
`/secrets/${this.ID}/update?version=${this.Version.Index}`,
{
Name: this.Spec.Name,
Labels: this.Spec.Labels,
2020-09-30 16:35:24 +00:00
Data: plugins.smartstring.base64.encode(contentArg),
},
2019-09-13 18:20:12 +02:00
);
2019-09-12 14:45:36 +02:00
}
/**
* Removes this secret from the Docker daemon
*/
2019-09-13 18:20:12 +02:00
public async remove() {
2019-09-12 14:45:36 +02:00
await this.dockerHost.request('DELETE', `/secrets/${this.ID}`);
}
2019-09-13 14:40:38 +02:00
/**
* Gets the version label of this secret
*/
2019-09-13 14:45:35 +02:00
public async getVersion() {
2019-09-13 14:40:38 +02:00
return this.Spec.Labels.version;
}
2019-09-13 18:20:12 +02:00
}