Skip to content

Commit

Permalink
Rename the command to 'cacert:get'
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Morhun <mmorhun@redhat.com>
  • Loading branch information
mmorhun committed Apr 27, 2020
1 parent 4567a9d commit dd1748e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 103 deletions.
67 changes: 32 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ USAGE
# Commands
<!-- commands -->
* [`chectl autocomplete [SHELL]`](#chectl-autocomplete-shell)
* [`chectl cacert:get`](#chectl-cacertget)
* [`chectl dashboard:open`](#chectl-dashboardopen)
* [`chectl devfile:generate`](#chectl-devfilegenerate)
* [`chectl help [COMMAND]`](#chectl-help-command)
* [`chectl server:certificate`](#chectl-servercertificate)
* [`chectl server:debug`](#chectl-serverdebug)
* [`chectl server:delete`](#chectl-serverdelete)
* [`chectl server:logs`](#chectl-serverlogs)
Expand Down Expand Up @@ -124,6 +124,37 @@ EXAMPLES

_See code: [@oclif/plugin-autocomplete](https://github.com/oclif/plugin-autocomplete/blob/v0.1.5/src/commands/autocomplete/index.ts)_

## `chectl cacert:get`

Retrieves Eclipse Che self-signed certificate

```
USAGE
$ chectl cacert:get
OPTIONS
-d, --destination=destination
[default: ~] Destination where to store Che certificate.
If the destination is a file (might not exist), then the certificate will be saved there in PEM
format.
If the destination is a directory, then cheCA.crt file will be created there with Che
certificate in PEM format.
If this option is ommited, then Che certificate will be stored in user's home directory as
cheCA.crt
-h, --help
show CLI help
-n, --chenamespace=chenamespace
[default: che] Kubernetes namespace where Eclipse Che server is supposed to be deployed
-p, --platform=minikube|minishift|k8s|openshift|microk8s|docker-desktop|crc
Type of Kubernetes platform. Valid values are "minikube", "minishift", "k8s (for kubernetes)", "openshift", "crc
(for CodeReady Containers)", "microk8s".
```

_See code: [src/commands/cacert/get.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/cacert/get.ts)_

## `chectl dashboard:open`

Open Eclipse Che dashboard
Expand Down Expand Up @@ -190,40 +221,6 @@ OPTIONS

_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.2.3/src/commands/help.ts)_

## `chectl server:certificate`

Retrieves Eclipse Che self-signed certificate

```
USAGE
$ chectl server:certificate
OPTIONS
-d, --destination=destination
[default: ~] Destination where to store Che certificate.
If the destination is a file (might not exist), then the certificate will be saved there in PEM
format.
If the destination is a directory, then cheCA.crt file will be created there with Che
certificate in PEM format.
If this option is ommited, then Che certificate will be stored in user's home directory as
cheCA.crt
-h, --help
show CLI help
-n, --chenamespace=chenamespace
[default: che] Kubernetes namespace where Eclipse Che server is supposed to be deployed
-p, --platform=minikube|minishift|k8s|openshift|microk8s|docker-desktop|crc
Type of Kubernetes platform. Valid values are "minikube", "minishift", "k8s (for kubernetes)", "openshift", "crc
(for CodeReady Containers)", "microk8s".
--make-path
Creates path specified in "destination" parameter if it doesn't exist.
```

_See code: [src/commands/server/certificate.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/certificate.ts)_

## `chectl server:debug`

Enable local debug of Eclipse Che server
Expand Down
17 changes: 17 additions & 0 deletions src/api/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as yaml from 'js-yaml'
import * as path from 'path'

import { OpenShiftHelper } from '../api/openshift'
import { CHE_ROOT_CA_SECRET_NAME } from '../constants'

import { Devfile } from './devfile'
import { KubeHelper } from './kube'
Expand Down Expand Up @@ -107,6 +108,22 @@ export class CheHelper {
}
}

/**
* Gets self-signed Che CA certificate from 'self-signed-certificate' secret. The secret should exist.
*/
async retrieveEclipseCheCaCert(cheNamespace: string): Promise<string> {
const cheCaSecret = await this.kube.getSecret(CHE_ROOT_CA_SECRET_NAME, cheNamespace)
if (!cheCaSecret) {
throw new Error('Local Che CA self-signed certificate not found. Are you using self-signed certificate?')
}

if (cheCaSecret.data && cheCaSecret.data['ca.crt']) {
return Buffer.from(cheCaSecret.data['ca.crt'], 'base64').toString('ascii')
}

throw new Error(`Secret "${CHE_ROOT_CA_SECRET_NAME}" has invalid format.`)
}

async cheK8sURL(namespace = ''): Promise<string> {
const ingress_names = ['che', 'che-ingress']
for (const ingress_name of ingress_names) {
Expand Down
37 changes: 10 additions & 27 deletions src/commands/server/certificate.ts → src/commands/cacert/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
**********************************************************************/

import { Command, flags } from '@oclif/command'
import { boolean, string } from '@oclif/parser/lib/flags'
import { string } from '@oclif/parser/lib/flags'
import * as fs from 'fs'
import * as Listr from 'listr'
import * as os from 'os'
import * as path from 'path'

import { CheHelper } from '../../api/che'
import { cheNamespace } from '../../common-flags'
import { CheTasks } from '../../tasks/che'
import { ApiTasks } from '../../tasks/platforms/api'
import { PlatformTasks } from '../../tasks/platforms/platform'

const DEFAULT_CA_CERT_FILE_NAME = 'cheCA.crt'

export default class Certificate extends Command {
export default class Get extends Command {
static description = 'Retrieves Eclipse Che self-signed certificate'

static flags = {
Expand All @@ -42,30 +43,27 @@ export default class Certificate extends Command {
env: 'CHE_CA_CERT_LOCATION',
default: '~'
}),
'make-path': boolean({
description: 'Creates path specified in "destination" parameter if it doesn\'t exist.',
default: false
}),
}

async run() {
const { flags } = this.parse(Certificate)
const { flags } = this.parse(Get)
const ctx: any = {}
const cheHelper = new CheHelper(flags)
const platformTasks = new PlatformTasks()
const cheTasks = new CheTasks(flags)
const apiTasks = new ApiTasks()
const tasks = new Listr([], { renderer: 'silent' })

const targetFile = this.prepareTarget(flags.destination, flags['make-path'])
ctx.cheCaCertFile = targetFile
const targetFile = this.prepareTarget(flags.destination)

tasks.add(platformTasks.preflightCheckTasks(flags, this))
tasks.add(apiTasks.testApiTasks(flags, this))
tasks.add(cheTasks.verifyCheNamespaceExistsTask(flags, this))
tasks.add(cheTasks.retrieveEclipseCheCaCert(flags))

try {
await tasks.run(ctx)
const cheCaCert = await cheHelper.retrieveEclipseCheCaCert(flags.chenamespace)
fs.writeFileSync(targetFile, cheCaCert)
this.log(`Eclipse Che self-signed CA certificate is exported to ${targetFile}`)
} catch (error) {
this.error(error)
Expand All @@ -75,7 +73,7 @@ export default class Certificate extends Command {
/**
* Handles certificate target location and returns string which points to the target file.
*/
private prepareTarget(destinaton: string, makePath = false): string {
private prepareTarget(destinaton: string): string {
if (destinaton === '~') {
return path.join(os.homedir(), DEFAULT_CA_CERT_FILE_NAME)
}
Expand All @@ -84,22 +82,7 @@ export default class Certificate extends Command {
return fs.lstatSync(destinaton).isDirectory() ? path.join(destinaton, DEFAULT_CA_CERT_FILE_NAME) : destinaton
}

const baseDirectory = path.dirname(destinaton)
if (fs.existsSync(baseDirectory)) {
return destinaton
}

if (makePath) {
if (destinaton.endsWith('/')) {
fs.mkdirSync(destinaton, { recursive: true })
return path.join(destinaton, DEFAULT_CA_CERT_FILE_NAME)
} else {
fs.mkdirSync(baseDirectory, { recursive: true })
return destinaton
}
} else {
throw new Error(`Base directory "${baseDirectory}" doesn't exist.`)
}
throw new Error('Given certificate path doesn\'t exist.')
}

}
41 changes: 0 additions & 41 deletions src/tasks/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import { Command } from '@oclif/command'
import * as fs from 'fs'
import * as Listr from 'listr'

import { CheHelper } from '../api/che'
import { KubeHelper } from '../api/kube'
import { OpenShiftHelper } from '../api/openshift'
import { CHE_ROOT_CA_SECRET_NAME } from '../constants'

import { KubeTasks } from './kube'

Expand Down Expand Up @@ -606,45 +604,6 @@ export class CheTasks {
]
}

/**
* Saves self-signed Che CA certificate into file. 'self-signed-certificate' secret should exist.
*/
retrieveEclipseCheCaCert(flags: any): ReadonlyArray<Listr.ListrTask> {
return [
{
title: 'Retrieving self-signed Eclipse Che CA certificate',
task: async (ctx: any, task: any) => {
const cheCaSecret = await this.kube.getSecret(CHE_ROOT_CA_SECRET_NAME, flags.chenamespace)
if (!cheCaSecret) {
throw new Error(`Secret "${CHE_ROOT_CA_SECRET_NAME}" not found.`)
}
if (cheCaSecret.data && cheCaSecret.data['ca.crt']) {
ctx.cheCaCert = Buffer.from(cheCaSecret.data['ca.crt'], 'base64').toString('ascii')
} else {
throw new Error(`Secret "${CHE_ROOT_CA_SECRET_NAME}" has invalid format.`)
}

task.title = `${task.title}... done`
}
},
{
title: 'Saving self-signed Eclipse Che CA certificate',
task: async (ctx: any, task: any) => {
if (!ctx.cheCaCert) {
throw new Error('Che CA certificate is not present in the context.')
}
if (!ctx.cheCaCertFile) {
throw new Error('Target file for Che CA certificate is not present in the context.')
}

fs.writeFileSync(ctx.cheCaCertFile, ctx.cheCaCert)

task.title = `Eclipse Che self-signed CA certificate is saved at ${ctx.cheCaCertFile}`
}
}
]
}

checkEclipseCheStatus(): ReadonlyArray<Listr.ListrTask> {
return [
{
Expand Down

0 comments on commit dd1748e

Please sign in to comment.