# All files / src/services/jenkins-auth config-jenkins-auth.ts

91.89% Statements 34/37
76.67% Branches 23/30
87.5% Functions 7/8
91.89% Lines 34/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    1381x

    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    5x
    1x

    4x

    4x

    4x

    4x

    4x
    2x

    4x
    3x

    4x

    2x

    2x

    1x

    1x

     import {Container, Inject} from 'typescript-ioc';

    import {JenkinsAuth} from './config-jenkins-auth.api';
    import {JenkinsAuthOptions} from './config-jenkins-auth-options.model';
    import {GenerateToken, GenerateTokenOptions} from '../generate-jenkins-token';
    import {KubeSecret} from '@ibmgaragecloud/cloud-native-toolkit-cli/dist/api/kubectl/secrets';
    import {KubeIngress} from '@ibmgaragecloud/cloud-native-toolkit-cli/dist/api/kubectl/ingress';
    import {
      DefaultBackend,
      InClusterBackend
    } from '@ibmgaragecloud/cloud-native-toolkit-cli/dist/api/kubectl/client.backend';
    import {KubeBackend} from '@ibmgaragecloud/cloud-native-toolkit-cli/dist/api/kubectl/client.api';

    interface JenkinsSecret {
      'jenkins-admin-password': string;
      'jenkins-admin-user': string;
    }

    interface GenerateAuthSecret {
      host: string;
      url: string;
      username: string;
      password: string;
      apiToken: string;
      namespace?: string;
    }

    const noopNotifyStatus: (status: string) => void = () => {};

    export class JenkinsAuthImpl implements JenkinsAuth {
      @Inject
      private kubeSecret: KubeSecret;
      @Inject
      private kubeIngress: KubeIngress;
      @Inject
      private generateToken: GenerateToken;

      isAvailable(): boolean {
        return this.generateToken && this.generateToken.isAvailable && this.generateToken.isAvailable();
      }

      async configJenkinsAuth(options: JenkinsAuthOptions, notifyStatus: (status: string) => void = noopNotifyStatus) {
        Iif (options.debug) {
          console.log('options: ', options);
        }

        this.configureKubernetesBackend(options);

        const {username, password} = await this.retrieveJenkinsCredentials(options, notifyStatus);

        const {host, url} = await this.retrieveJenkinsUrl(options, notifyStatus);

        const apiToken = await this.retrieveJenkinsApiToken(
          {url, username, password},
          notifyStatus,
        );

        return this.generateJenkinsAuthSecret(
          {host, url, username, password, apiToken},
          notifyStatus,
        );
      }

      configureKubernetesBackend({inCluster}: {inCluster?: boolean} = {inCluster: false}) {
        Iif (inCluster) {
          Container.bind(KubeBackend).to(InClusterBackend);
        } else {
          Container.bind(KubeBackend).to(DefaultBackend);
        }
      }

      async retrieveJenkinsCredentials({username, password, namespace = 'tools'}: {username?: string; password?: string; namespace?: string} = {}, notifyStatus: (status: string) => void = noopNotifyStatus): Promise<{username: string; password: string}> {

        if (username && password) {
          return {username, password};
        }

        notifyStatus(`Retrieving Jenkins admin password`);

        const jenkinsSecret: JenkinsSecret = await this.kubeSecret.getData('jenkins', namespace);

        return {
          username: jenkinsSecret['jenkins-admin-user'],
          password: jenkinsSecret['jenkins-admin-password']
        };
      }

      async retrieveJenkinsUrl({url, host, namespace = 'tools'}: {url?: string, host?: string, namespace?: string} = {}, notifyStatus: (status: string) => void = noopNotifyStatus): Promise<{host: string; url: string}> {
        notifyStatus(`Retrieving Jenkins url`);

        if (!host) {
          host = (await this.kubeIngress.getHosts(namespace, 'jenkins'))[0];
        }

        if (!url) {
          url = (await this.kubeIngress.getUrls(namespace, 'jenkins'))[0];
        }

        return {host, url: url || `http://${host}`};
      }

      async retrieveJenkinsApiToken(options: GenerateTokenOptions & {jenkinsApiToken?: string}, notifyStatus: (status: string) => void = noopNotifyStatus): Promise<string> {

        notifyStatus(`Generating Jenkins api token: ${options.url}`);

        return options.jenkinsApiToken || await this.generateToken.generateToken(options, notifyStatus);
      }

      async generateJenkinsAuthSecret({host, url, username, password, apiToken, namespace = 'tools'}: GenerateAuthSecret, notifyStatus: (status: string) => void = noopNotifyStatus) {

        notifyStatus(`Generating jenkins-access secret using apiToken: ${apiToken}`);

        return this.kubeSecret.createOrUpdate(
          'jenkins-access',
          {
            body: {
              apiVersion: 'v1',
              kind: 'Secret',
              metadata: {
                name: 'jenkins-access',
                annotations: {
                  description: 'secret providing jenkins credentials, used to interact with Jenkins apis',
                },
              },
              type: 'Opaque',
              stringData: {
                username,
                password,
                api_token: apiToken,
                url,
                host,
              },
            }
          },
          namespace);
      }
    }