policy_editor.vue 8.28 KB
Newer Older
1
<script>
2
import { mapState, mapActions } from 'vuex';
3
4
5
6
7
8
9
10
import {
  GlFormGroup,
  GlFormSelect,
  GlFormInput,
  GlFormTextarea,
  GlToggle,
  GlSegmentedControl,
  GlButton,
11
  GlAlert,
12
13
} from '@gitlab/ui';
import { s__ } from '~/locale';
14
import { redirectTo } from '~/lib/utils/url_utility';
15
16
17
18
19
import EnvironmentPicker from '../environment_picker.vue';
import NetworkPolicyEditor from '../network_policy_editor.vue';
import PolicyRuleBuilder from './policy_rule_builder.vue';
import PolicyPreview from './policy_preview.vue';
import PolicyActionPicker from './policy_action_picker.vue';
20
21
22
23
24
25
import {
  EditorModeRule,
  EditorModeYAML,
  EndpointMatchModeAny,
  RuleTypeEndpoint,
} from './constants';
26
import toYaml from './lib/to_yaml';
27
import fromYaml from './lib/from_yaml';
28
import { buildRule } from './lib/rules';
29
import humanizeNetworkPolicy from './lib/humanize';
30
31
32
33
34
35
36
37
38
39

export default {
  components: {
    GlFormGroup,
    GlFormSelect,
    GlFormInput,
    GlFormTextarea,
    GlToggle,
    GlSegmentedControl,
    GlButton,
40
    GlAlert,
41
42
43
44
45
46
    EnvironmentPicker,
    NetworkPolicyEditor,
    PolicyRuleBuilder,
    PolicyPreview,
    PolicyActionPicker,
  },
47
48
49
50
51
52
  props: {
    threatMonitoringPath: {
      type: String,
      required: true,
    },
  },
53
  data() {
54
55
    return {
      editorMode: EditorModeRule,
56
57
      yamlEditorValue: '',
      yamlEditorError: null,
58
59
60
61
62
63
64
65
66
      policy: {
        name: '',
        description: '',
        isEnabled: false,
        endpointMatchMode: EndpointMatchModeAny,
        endpointLabels: '',
        rules: [],
      },
    };
67
68
  },
  computed: {
69
70
71
72
73
74
    humanizedPolicy() {
      return humanizeNetworkPolicy(this.policy);
    },
    policyYaml() {
      return toYaml(this.policy);
    },
75
76
    ...mapState('threatMonitoring', ['currentEnvironmentId']),
    ...mapState('networkPolicies', ['errorUpdatingPolicy']),
77
78
79
80
81
82
    shouldShowRuleEditor() {
      return this.editorMode === EditorModeRule;
    },
    shouldShowYamlEditor() {
      return this.editorMode === EditorModeYAML;
    },
83
84
85
    hasParsingError() {
      return Boolean(this.yamlEditorError);
    },
86
  },
87
88
89
90
91
  created() {
    this.fetchEnvironments();
  },
  methods: {
    ...mapActions('threatMonitoring', ['fetchEnvironments']),
92
    ...mapActions('networkPolicies', ['createPolicy']),
93
94
95
96
97
98
99
100
101
102
103
104
105
    addRule() {
      this.policy.rules.push(buildRule(RuleTypeEndpoint));
    },
    updateEndpointMatchMode(mode) {
      this.policy.endpointMatchMode = mode;
    },
    updateEndpointLabels(labels) {
      this.policy.endpointLabels = labels;
    },
    updateRuleType(ruleIdx, ruleType) {
      const rule = this.policy.rules[ruleIdx];
      this.policy.rules.splice(ruleIdx, 1, buildRule(ruleType, rule));
    },
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    loadYaml(manifest) {
      this.yamlEditorValue = manifest;
      this.yamlEditorError = null;

      try {
        Object.assign(this.policy, fromYaml(manifest));
      } catch (error) {
        this.yamlEditorError = error;
      }
    },
    changeEditorMode(mode) {
      if (mode === EditorModeYAML) {
        this.yamlEditorValue = toYaml(this.policy);
      }

      this.editorMode = mode;
    },
123
124
125
126
127
128
129
130
131
    savePolicy() {
      const policy = { manifest: toYaml(this.policy) };
      return this.createPolicy({
        environmentId: this.currentEnvironmentId,
        policy,
      }).then(() => {
        if (!this.errorUpdatingPolicy) redirectTo(this.threatMonitoringPath);
      });
    },
132
  },
133
134
135
136
137
  policyTypes: [{ value: 'networkPolicy', text: s__('NetworkPolicies|Network Policy') }],
  editorModes: [
    { value: EditorModeRule, text: s__('NetworkPolicies|Rule mode') },
    { value: EditorModeYAML, text: s__('NetworkPolicies|.yaml mode') },
  ],
138
139
140
  parsingErrorMessage: s__(
    'NetworkPolicies|Rule mode is unavailable for this policy. In some cases, we cannot parse the YAML file back into the rules editor.',
  ),
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
};
</script>

<template>
  <section>
    <header class="my-3">
      <h2 class="h3 mb-1">
        {{ s__('NetworkPolicies|Policy description') }}
      </h2>
    </header>

    <div class="row">
      <div class="col-sm-6 col-md-4 col-lg-3 col-xl-2">
        <gl-form-group :label="s__('NetworkPolicies|Policy type')" label-for="policyType">
          <gl-form-select
            id="policyType"
            value="networkPolicy"
            :options="$options.policyTypes"
            disabled
          />
        </gl-form-group>
      </div>
      <div class="col-sm-6 col-md-6 col-lg-5 col-xl-4">
        <gl-form-group :label="s__('NetworkPolicies|Name')" label-for="policyName">
165
          <gl-form-input id="policyName" v-model="policy.name" />
166
167
168
169
170
171
        </gl-form-group>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12 col-md-10 col-lg-8 col-xl-6">
        <gl-form-group :label="s__('NetworkPolicies|Description')" label-for="policyDescription">
172
          <gl-form-textarea id="policyDescription" v-model="policy.description" />
173
174
175
176
177
178
179
180
181
        </gl-form-group>
      </div>
    </div>
    <div class="row">
      <environment-picker />
    </div>
    <div class="row">
      <div class="col-md-auto">
        <gl-form-group :label="s__('NetworkPolicies|Policy status')" label-for="policyStatus">
182
          <gl-toggle id="policyStatus" v-model="policy.isEnabled" />
183
184
185
186
187
188
        </gl-form-group>
      </div>
    </div>
    <div class="row">
      <div class="col-md-auto">
        <gl-form-group :label="s__('NetworkPolicies|Editor mode')" label-for="editorMode">
189
190
191
192
193
194
          <gl-segmented-control
            data-testid="editor-mode"
            :options="$options.editorModes"
            :checked="editorMode"
            @input="changeEditorMode"
          />
195
196
197
198
199
200
        </gl-form-group>
      </div>
    </div>
    <hr />
    <div v-if="shouldShowRuleEditor" class="row" data-testid="rule-editor">
      <div class="col-sm-12 col-md-6 col-lg-7 col-xl-8">
201
202
203
204
        <gl-alert v-if="hasParsingError" data-testid="parsing-alert" :dismissible="false">{{
          $options.parsingErrorMessage
        }}</gl-alert>

205
        <h4>{{ s__('NetworkPolicies|Rules') }}</h4>
206
207
208
209
210
211
212
213
214
215
216
217
        <policy-rule-builder
          v-for="(rule, idx) in policy.rules"
          :key="idx"
          class="gl-mb-4"
          :rule="rule"
          :endpoint-match-mode="policy.endpointMatchMode"
          :endpoint-labels="policy.endpointLabels"
          :endpoint-selector-disabled="idx > 0"
          @rule-type-change="updateRuleType(idx, $event)"
          @endpoint-match-mode-change="updateEndpointMatchMode"
          @endpoint-labels-change="updateEndpointLabels"
        />
218

219
        <div class="gl-p-3 gl-rounded-base gl-border-1 gl-border-solid gl-border-gray-100 gl-mb-5">
220
221
222
223
224
225
226
227
          <gl-button
            variant="link"
            category="primary"
            data-testid="add-rule"
            :disabled="hasParsingError"
            @click="addRule"
            >{{ s__('Network Policy|New rule') }}</gl-button
          >
228
229
230
        </div>

        <h4>{{ s__('NetworkPolicies|Actions') }}</h4>
231
        <p>{{ s__('NetworkPolicies|Traffic that does not match any rule will be blocked.') }}</p>
232
233
234
235
        <policy-action-picker />
      </div>
      <div class="col-sm-12 col-md-6 col-lg-5 col-xl-4">
        <h5>{{ s__('NetworkPolicies|Policy preview') }}</h5>
236
        <policy-preview :policy-yaml="policyYaml" :policy-description="humanizedPolicy" />
237
238
239
240
241
      </div>
    </div>
    <div v-if="shouldShowYamlEditor" class="row" data-testid="yaml-editor">
      <div class="col-sm-12 col-md-12 col-lg-10 col-xl-8">
        <div class="gl-rounded-base gl-border-1 gl-border-solid gl-border-gray-100">
242
243
244
          <h5
            class="gl-m-0 gl-p-4 gl-bg-gray-10 gl-border-1 gl-border-b-solid gl-border-b-gray-100"
          >
245
246
            {{ s__('NetworkPolicies|YAML editor') }}
          </h5>
247
248
249
250
251
252
253
254
          <div class="gl-p-4">
            <network-policy-editor
              :value="yamlEditorValue"
              :height="400"
              :read-only="false"
              @input="loadYaml"
            />
          </div>
255
256
257
258
259
260
        </div>
      </div>
    </div>
    <hr />
    <div class="row">
      <div class="col-md-auto">
261
262
263
264
265
266
267
268
269
270
        <gl-button
          type="submit"
          category="primary"
          variant="success"
          data-testid="create-policy"
          @click="savePolicy"
          >{{ s__('NetworkPolicies|Create policy') }}</gl-button
        >
        <gl-button category="secondary" variant="default" :href="threatMonitoringPath">{{
          __('Cancel')
271
272
273
274
275
        }}</gl-button>
      </div>
    </div>
  </section>
</template>