summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/token_access/components/opt_in_jwt.vue
blob: c774f37b1e44ca1f2a8734566c995266c9c91c1b (plain)
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
<script>
import { GlLink, GlLoadingIcon, GlSprintf, GlToggle } from '@gitlab/ui';
import CodeInstruction from '~/vue_shared/components/registry/code_instruction.vue';
import { createAlert } from '~/flash';
import { __, s__ } from '~/locale';
import updateOptInJwtMutation from '../graphql/mutations/update_opt_in_jwt.mutation.graphql';
import getOptInJwtSettingQuery from '../graphql/queries/get_opt_in_jwt_setting.query.graphql';
import { LIMIT_JWT_ACCESS_SNIPPET, OPT_IN_JWT_HELP_LINK } from '../constants';

export default {
  i18n: {
    labelText: s__('CICD|Limit JSON Web Token (JWT) access'),
    helpText: s__(
      `CICD|The JWT must be manually declared in each job that needs it. When disabled, the token is always available in all jobs in the pipeline. %{linkStart}Learn more.%{linkEnd}`,
    ),
    expandedText: s__(
      'CICD|Use the %{codeStart}secrets%{codeEnd} keyword to configure a job with a JWT.',
    ),
    copyToClipboard: __('Copy to clipboard'),
    fetchError: s__('CICD|There was a problem fetching the token access settings.'),
    updateError: s__('CICD|An error occurred while update the setting. Please try again.'),
  },
  components: {
    CodeInstruction,
    GlLink,
    GlLoadingIcon,
    GlSprintf,
    GlToggle,
  },
  inject: ['fullPath'],
  apollo: {
    optInJwt: {
      query: getOptInJwtSettingQuery,
      variables() {
        return {
          fullPath: this.fullPath,
        };
      },
      update({
        project: {
          ciCdSettings: { optInJwt },
        },
      }) {
        return optInJwt;
      },
      error() {
        createAlert({ message: this.$options.i18n.fetchError });
      },
    },
  },
  data() {
    return {
      optInJwt: null,
    };
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.optInJwt.loading;
    },
  },
  methods: {
    async updateOptInJwt() {
      try {
        const {
          data: {
            ciCdSettingsUpdate: { errors },
          },
        } = await this.$apollo.mutate({
          mutation: updateOptInJwtMutation,
          variables: {
            input: {
              fullPath: this.fullPath,
              optInJwt: this.optInJwt,
            },
          },
        });

        if (errors.length) {
          throw new Error(errors[0]);
        }
      } catch (error) {
        createAlert({ message: this.$options.i18n.updateError });
      }
    },
  },
  OPT_IN_JWT_HELP_LINK,
  LIMIT_JWT_ACCESS_SNIPPET,
};
</script>
<template>
  <div>
    <gl-loading-icon v-if="isLoading" size="lg" class="gl-mt-5" />
    <template v-else>
      <gl-toggle
        v-model="optInJwt"
        class="gl-mt-5"
        :label="$options.i18n.labelText"
        @change="updateOptInJwt"
      >
        <template #help>
          <gl-sprintf :message="$options.i18n.helpText">
            <template #link="{ content }">
              <gl-link :href="$options.OPT_IN_JWT_HELP_LINK" class="inline-link" target="_blank">
                {{ content }}
              </gl-link>
            </template>
          </gl-sprintf>
        </template>
      </gl-toggle>
      <div v-if="optInJwt" class="gl-mt-5" data-testid="opt-in-jwt-expanded-section">
        <gl-sprintf :message="$options.i18n.expandedText">
          <template #code="{ content }">
            <code>{{ content }}</code>
          </template>
        </gl-sprintf>
        <code-instruction
          class="gl-mt-3"
          :instruction="$options.LIMIT_JWT_ACCESS_SNIPPET"
          :copy-text="$options.i18n.copyToClipboard"
          multiline
        />
      </div>
    </template>
  </div>
</template>