Commit e46499c5 authored by Alexander Turinske's avatar Alexander Turinske

Refactor network policy editor to be more generic

- generalize policy editor to work for policies that
  are not the network type
- move network-policy-editor to its own folder
- rename files to reflect more generalized nature
- update tests
parent 4055b8fe
<script> <script>
import { __ } from '~/locale'; import { __ } from '~/locale';
import fromYaml, { removeUnnecessaryDashes } from '../policy_editor/lib/from_yaml'; import {
import humanizeNetworkPolicy from '../policy_editor/lib/humanize'; fromYaml,
humanizeNetworkPolicy,
removeUnnecessaryDashes,
} from '../policy_editor/network_policy/lib';
import PolicyPreview from '../policy_editor/policy_preview.vue'; import PolicyPreview from '../policy_editor/policy_preview.vue';
export default { export default {
......
<script> <script>
import { GlButton, GlDrawer } from '@gitlab/ui'; import { GlButton, GlDrawer } from '@gitlab/ui';
import { getContentWrapperHeight } from '../../utils'; import { getContentWrapperHeight } from '../../utils';
import { CiliumNetworkPolicyKind } from '../policy_editor/constants'; import { CiliumNetworkPolicyKind } from '../policy_editor/network_policy/lib';
import CiliumNetworkPolicy from './cilium_network_policy.vue'; import CiliumNetworkPolicy from './cilium_network_policy.vue';
export default { export default {
components: { components: {
GlButton, GlButton,
GlDrawer, GlDrawer,
NetworkPolicyEditor: () => PolicyYamlEditor: () =>
import(/* webpackChunkName: 'network_policy_editor' */ '../network_policy_editor.vue'), import(/* webpackChunkName: 'policy_yaml_editor' */ '../policy_yaml_editor.vue'),
CiliumNetworkPolicy, CiliumNetworkPolicy,
}, },
props: { props: {
...@@ -70,9 +70,9 @@ export default { ...@@ -70,9 +70,9 @@ export default {
{{ s__("NetworkPolicies|Define this policy's location, conditions and actions.") }} {{ s__("NetworkPolicies|Define this policy's location, conditions and actions.") }}
</p> </p>
<div class="gl-p-3 gl-bg-gray-50"> <div class="gl-p-3 gl-bg-gray-50">
<network-policy-editor <policy-yaml-editor
:value="policy.manifest" :value="policy.manifest"
data-testid="policyEditor" data-testid="policy-yaml-editor"
class="network-policy-editor" class="network-policy-editor"
/> />
</div> </div>
......
...@@ -3,41 +3,14 @@ import { s__ } from '~/locale'; ...@@ -3,41 +3,14 @@ import { s__ } from '~/locale';
export const EditorModeRule = 'rule'; export const EditorModeRule = 'rule';
export const EditorModeYAML = 'yaml'; export const EditorModeYAML = 'yaml';
export const RuleTypeNetwork = 'network';
export const RuleActionTypeAllow = 'allow';
export const RuleDirectionInbound = 'ingress';
export const RuleDirectionOutbound = 'egress';
export const EndpointMatchModeAny = 'any';
export const EndpointMatchModeLabel = 'label';
export const RuleTypeEndpoint = 'NetworkPolicyRuleEndpoint';
export const RuleTypeEntity = 'NetworkPolicyRuleEntity';
export const RuleTypeCIDR = 'NetworkPolicyRuleCIDR';
export const RuleTypeFQDN = 'NetworkPolicyRuleFQDN';
export const EntityTypes = {
ALL: 'all',
HOST: 'host',
REMOTE_NODE: 'remote-node',
CLUSTER: 'cluster',
INIT: 'init',
HEALTH: 'health',
UNMANAGED: 'unmanaged',
WORLD: 'world',
};
export const PortMatchModeAny = 'any';
export const PortMatchModePortProtocol = 'port/protocol';
export const DisabledByLabel = 'network-policy.gitlab.com/disabled_by';
export const CiliumNetworkPolicyKind = 'CiliumNetworkPolicy';
export const ProjectIdLabel = 'app.gitlab.com/proj';
export const PARSING_ERROR_MESSAGE = s__( export const PARSING_ERROR_MESSAGE = s__(
'NetworkPolicies|Rule mode is unavailable for this policy. In some cases, we cannot parse the YAML file back into the rules editor.', 'NetworkPolicies|Rule mode is unavailable for this policy. In some cases, we cannot parse the YAML file back into the rules editor.',
); );
export const POLICY_TYPES = {
networkPolicy: {
value: 'networkPolicy',
text: s__('NetworkPolicies|Network Policy'),
component: 'network-policy-editor',
},
};
export const RuleTypeNetwork = 'network';
export const RuleActionTypeAllow = 'allow';
export const RuleDirectionInbound = 'ingress';
export const RuleDirectionOutbound = 'egress';
export const EndpointMatchModeAny = 'any';
export const EndpointMatchModeLabel = 'label';
export const RuleTypeEndpoint = 'NetworkPolicyRuleEndpoint';
export const RuleTypeEntity = 'NetworkPolicyRuleEntity';
export const RuleTypeCIDR = 'NetworkPolicyRuleCIDR';
export const RuleTypeFQDN = 'NetworkPolicyRuleFQDN';
export const CiliumNetworkPolicyKind = 'CiliumNetworkPolicy';
export const EntityTypes = {
ALL: 'all',
HOST: 'host',
REMOTE_NODE: 'remote-node',
CLUSTER: 'cluster',
INIT: 'init',
HEALTH: 'health',
UNMANAGED: 'unmanaged',
WORLD: 'world',
};
export const PortMatchModeAny = 'any';
export const PortMatchModePortProtocol = 'port/protocol';
export const DisabledByLabel = 'network-policy.gitlab.com/disabled_by';
export const ProjectIdLabel = 'app.gitlab.com/proj';
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
RuleTypeEntity, RuleTypeEntity,
RuleTypeCIDR, RuleTypeCIDR,
RuleTypeFQDN, RuleTypeFQDN,
} from '../constants'; } from './constants';
import { buildRule } from './rules'; import { buildRule } from './rules';
/* /*
......
...@@ -6,7 +6,7 @@ import { ...@@ -6,7 +6,7 @@ import {
RuleTypeEntity, RuleTypeEntity,
RuleTypeCIDR, RuleTypeCIDR,
RuleTypeFQDN, RuleTypeFQDN,
} from '../constants'; } from './constants';
import { portSelectors, labelSelector, splitItems } from './utils'; import { portSelectors, labelSelector, splitItems } from './utils';
const strongArgs = { strongOpen: '<strong>', strongClose: '</strong>' }; const strongArgs = { strongOpen: '<strong>', strongClose: '</strong>' };
......
import { EndpointMatchModeAny } from './constants';
export * from './constants';
export { default as fromYaml, removeUnnecessaryDashes } from './from_yaml';
export { default as humanizeNetworkPolicy } from './humanize';
export { buildRule } from './rules';
export { default as toYaml } from './to_yaml';
export const DEFAULT_NETWORK_POLICY = {
name: '',
description: '',
isEnabled: false,
endpointMatchMode: EndpointMatchModeAny,
endpointLabels: '',
annotations: '',
labels: '',
};
...@@ -5,7 +5,7 @@ import { ...@@ -5,7 +5,7 @@ import {
RuleTypeFQDN, RuleTypeFQDN,
RuleDirectionInbound, RuleDirectionInbound,
PortMatchModeAny, PortMatchModeAny,
} from '../constants'; } from './constants';
import { portSelectors, labelSelector, splitItems } from './utils'; import { portSelectors, labelSelector, splitItems } from './utils';
/* /*
......
import { safeDump } from 'js-yaml'; import { safeDump } from 'js-yaml';
import { EndpointMatchModeAny, DisabledByLabel, CiliumNetworkPolicyKind } from '../constants'; import { EndpointMatchModeAny, DisabledByLabel, CiliumNetworkPolicyKind } from './constants';
import { ruleSpec } from './rules'; import { ruleSpec } from './rules';
import { labelSelector } from './utils'; import { labelSelector } from './utils';
......
import { PortMatchModeAny } from '../constants'; import { PortMatchModeAny } from './constants';
/* /*
Convert space separated list of labels into a kubernetes selector. Convert space separated list of labels into a kubernetes selector.
......
...@@ -13,7 +13,7 @@ import { ...@@ -13,7 +13,7 @@ import {
RuleTypeFQDN, RuleTypeFQDN,
PortMatchModeAny, PortMatchModeAny,
PortMatchModePortProtocol, PortMatchModePortProtocol,
} from './constants'; } from './lib';
import PolicyRuleCIDR from './policy_rule_cidr.vue'; import PolicyRuleCIDR from './policy_rule_cidr.vue';
import PolicyRuleEndpoint from './policy_rule_endpoint.vue'; import PolicyRuleEndpoint from './policy_rule_endpoint.vue';
import PolicyRuleEntity from './policy_rule_entity.vue'; import PolicyRuleEntity from './policy_rule_entity.vue';
......
<script> <script>
import { GlDropdown, GlDropdownItem } from '@gitlab/ui'; import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { sprintf, s__ } from '~/locale'; import { sprintf, s__ } from '~/locale';
import { EntityTypes } from './constants'; import { EntityTypes } from './lib';
export default { export default {
components: { components: {
......
<script> <script>
import { GlForm, GlFormSelect, GlSprintf } from '@gitlab/ui'; import { GlForm, GlFormSelect, GlSprintf } from '@gitlab/ui';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
import { RuleActionTypeAllow } from './constants'; import { RuleActionTypeAllow } from './network_policy/lib';
export default { export default {
components: { components: {
......
import CiliumNetworkPolicy from 'ee/threat_monitoring/components/policy_drawer/cilium_network_policy.vue'; import CiliumNetworkPolicy from 'ee/threat_monitoring/components/policy_drawer/cilium_network_policy.vue';
import toYaml from 'ee/threat_monitoring/components/policy_editor/lib/to_yaml'; import { toYaml } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
import PolicyPreview from 'ee/threat_monitoring/components/policy_editor/policy_preview.vue'; import PolicyPreview from 'ee/threat_monitoring/components/policy_editor/policy_preview.vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper'; import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
......
...@@ -15,13 +15,13 @@ describe('NetworkPolicyDrawer component', () => { ...@@ -15,13 +15,13 @@ describe('NetworkPolicyDrawer component', () => {
open: true, open: true,
...propsData, ...propsData,
}, },
stubs: { NetworkPolicyEditor: true }, stubs: { PolicyYamlEditor: true },
}); });
}; };
// Finders // Finders
const findEditButton = () => wrapper.findByTestId('edit-button'); const findEditButton = () => wrapper.findByTestId('edit-button');
const findPolicyEditor = () => wrapper.findByTestId('policyEditor'); const findPolicyEditor = () => wrapper.findByTestId('policy-yaml-editor');
const findCiliumNetworkPolicy = () => wrapper.findComponent(CiliumNetworkPolicy); const findCiliumNetworkPolicy = () => wrapper.findComponent(CiliumNetworkPolicy);
// Shared assertions // Shared assertions
......
...@@ -10,12 +10,11 @@ import { ...@@ -10,12 +10,11 @@ import {
RuleTypeCIDR, RuleTypeCIDR,
RuleTypeFQDN, RuleTypeFQDN,
EntityTypes, EntityTypes,
} from 'ee/threat_monitoring/components/policy_editor/constants'; fromYaml,
import fromYaml, {
removeUnnecessaryDashes, removeUnnecessaryDashes,
} from 'ee/threat_monitoring/components/policy_editor/lib/from_yaml'; buildRule,
import { buildRule } from 'ee/threat_monitoring/components/policy_editor/lib/rules'; toYaml,
import toYaml from 'ee/threat_monitoring/components/policy_editor/lib/to_yaml'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
describe('fromYaml', () => { describe('fromYaml', () => {
let policy; let policy;
......
...@@ -6,9 +6,9 @@ import { ...@@ -6,9 +6,9 @@ import {
RuleTypeEntity, RuleTypeEntity,
RuleTypeCIDR, RuleTypeCIDR,
RuleTypeFQDN, RuleTypeFQDN,
} from 'ee/threat_monitoring/components/policy_editor/constants'; humanizeNetworkPolicy,
import humanizeNetworkPolicy from 'ee/threat_monitoring/components/policy_editor/lib/humanize'; buildRule,
import { buildRule } from 'ee/threat_monitoring/components/policy_editor/lib/rules'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
describe('humanizeNetworkPolicy', () => { describe('humanizeNetworkPolicy', () => {
let policy; let policy;
......
...@@ -8,8 +8,9 @@ import { ...@@ -8,8 +8,9 @@ import {
PortMatchModeAny, PortMatchModeAny,
PortMatchModePortProtocol, PortMatchModePortProtocol,
EntityTypes, EntityTypes,
} from 'ee/threat_monitoring/components/policy_editor/constants'; buildRule,
import { buildRule, ruleSpec } from 'ee/threat_monitoring/components/policy_editor/lib/rules'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
import { ruleSpec } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib/rules';
describe('buildRule', () => { describe('buildRule', () => {
const oldRule = { const oldRule = {
......
import { EndpointMatchModeLabel } from 'ee/threat_monitoring/components/policy_editor/constants'; import {
import { buildRule } from 'ee/threat_monitoring/components/policy_editor/lib/rules'; EndpointMatchModeLabel,
import toYaml from 'ee/threat_monitoring/components/policy_editor/lib/to_yaml'; buildRule,
toYaml,
} from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
describe('toYaml', () => { describe('toYaml', () => {
let policy; let policy;
......
import { import {
PortMatchModeAny, PortMatchModeAny,
PortMatchModePortProtocol, PortMatchModePortProtocol,
} from 'ee/threat_monitoring/components/policy_editor/constants'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
import { import {
labelSelector, labelSelector,
portSelectors, portSelectors,
splitItems, splitItems,
} from 'ee/threat_monitoring/components/policy_editor/lib/utils'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib/utils';
describe('labelSelector', () => { describe('labelSelector', () => {
it('returns selector map', () => { it('returns selector map', () => {
......
...@@ -7,13 +7,13 @@ import { ...@@ -7,13 +7,13 @@ import {
RuleTypeCIDR, RuleTypeCIDR,
RuleTypeFQDN, RuleTypeFQDN,
PortMatchModePortProtocol, PortMatchModePortProtocol,
} from 'ee/threat_monitoring/components/policy_editor/constants'; buildRule,
import { buildRule } from 'ee/threat_monitoring/components/policy_editor/lib/rules'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
import PolicyRuleBuilder from 'ee/threat_monitoring/components/policy_editor/policy_rule_builder.vue'; import PolicyRuleBuilder from 'ee/threat_monitoring/components/policy_editor/network_policy/policy_rule_builder.vue';
import PolicyRuleCIDR from 'ee/threat_monitoring/components/policy_editor/policy_rule_cidr.vue'; import PolicyRuleCIDR from 'ee/threat_monitoring/components/policy_editor/network_policy/policy_rule_cidr.vue';
import PolicyRuleEndpoint from 'ee/threat_monitoring/components/policy_editor/policy_rule_endpoint.vue'; import PolicyRuleEndpoint from 'ee/threat_monitoring/components/policy_editor/network_policy/policy_rule_endpoint.vue';
import PolicyRuleEntity from 'ee/threat_monitoring/components/policy_editor/policy_rule_entity.vue'; import PolicyRuleEntity from 'ee/threat_monitoring/components/policy_editor/network_policy/policy_rule_entity.vue';
import PolicyRuleFQDN from 'ee/threat_monitoring/components/policy_editor/policy_rule_fqdn.vue'; import PolicyRuleFQDN from 'ee/threat_monitoring/components/policy_editor/network_policy/policy_rule_fqdn.vue';
describe('PolicyRuleBuilder component', () => { describe('PolicyRuleBuilder component', () => {
let wrapper; let wrapper;
......
import { GlDropdown, GlDropdownItem } from '@gitlab/ui'; import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { EntityTypes } from 'ee/threat_monitoring/components/policy_editor/constants'; import { EntityTypes } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
import PolicyRuleEntity from 'ee/threat_monitoring/components/policy_editor/policy_rule_entity.vue'; import PolicyRuleEntity from 'ee/threat_monitoring/components/policy_editor/network_policy/policy_rule_entity.vue';
describe('PolicyRuleEntity component', () => { describe('PolicyRuleEntity component', () => {
let wrapper; let wrapper;
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import NetworkPolicyEditor from 'ee/threat_monitoring/components/network_policy_editor.vue'; import PolicyYamlEditor from 'ee/threat_monitoring/components/policy_yaml_editor.vue';
import EditorLite from '~/vue_shared/components/editor_lite.vue'; import EditorLite from '~/vue_shared/components/editor_lite.vue';
describe('NetworkPolicyEditor component', () => { describe('PolicyYamlEditor component', () => {
let wrapper; let wrapper;
const findEditor = () => wrapper.findComponent(EditorLite); const findEditor = () => wrapper.findComponent(EditorLite);
const factory = ({ propsData } = {}) => { const factory = ({ propsData } = {}) => {
wrapper = shallowMount(NetworkPolicyEditor, { wrapper = shallowMount(PolicyYamlEditor, {
propsData: { propsData: {
value: 'foo', value: 'foo',
...propsData, ...propsData,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment