Commit 49551dbc authored by GitLab Bot's avatar GitLab Bot

Automatic merge of gitlab-org/gitlab master

parents b51951ef af2320c7
......@@ -34,6 +34,12 @@ const addExperimentContext = (opts) => {
return options;
};
const renameKey = (o, oldKey, newKey) => {
const ret = {};
delete Object.assign(ret, o, { [newKey]: o[oldKey] })[oldKey];
return ret;
};
const createEventPayload = (el, { suffix = '' } = {}) => {
const {
trackAction,
......@@ -186,15 +192,18 @@ export default class Tracking {
(context) => context.schema !== standardContext.schema,
);
const mappedConfig = {
forms: { whitelist: config.forms?.allow || [] },
fields: { whitelist: config.fields?.allow || [] },
};
const mappedConfig = {};
if (config.forms) mappedConfig.forms = renameKey(config.forms, 'allow', 'whitelist');
if (config.fields) mappedConfig.fields = renameKey(config.fields, 'allow', 'whitelist');
const enabler = () => window.snowplow('enableFormTracking', mappedConfig, userProvidedContexts);
if (document.readyState !== 'loading') enabler();
else document.addEventListener('DOMContentLoaded', enabler);
if (document.readyState === 'complete') enabler();
else {
document.addEventListener('readystatechange', () => {
if (document.readyState === 'complete') enabler();
});
}
}
static mixin(opts = {}) {
......
This diff is collapsed.
......@@ -3,6 +3,7 @@
module NetworkPolicies
class ResourcesService
include NetworkPolicies::Responses
include Gitlab::Utils::UsageData
LIMIT = 100
......@@ -26,6 +27,12 @@ module NetworkPolicies
private
def track_usage_data_for_cluster(platform, policies)
return if policies.empty?
track_usage_event(:clusters_using_network_policies_ui, platform.cluster_id)
end
def execute_per_environment(platform, namespace)
policies = platform.kubeclient
.get_network_policies(namespace: namespace)
......@@ -33,6 +40,7 @@ module NetworkPolicies
policies += platform.kubeclient
.get_cilium_network_policies(namespace: namespace)
.map { |resource| Gitlab::Kubernetes::CiliumNetworkPolicy.from_resource(resource) }
track_usage_data_for_cluster(platform, policies)
[policies, nil]
rescue Kubeclient::HttpError => e
[policies, e]
......
---
key_path: redis_hll_counters.network_policies.clusters_using_network_policies_ui_monthly
description: Monthly number of distinct clusters with network policies using the network policies UI
product_section: sec
product_stage: protect
product_group: group::container security
product_category: container_network_security
value_type: number
status: implemented
milestone: "14.1"
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64731
time_frame: 28d
data_source: redis_hll
data_category: Operational
distribution:
- ee
tier:
- ultimate
---
key_path: redis_hll_counters.network_policies.clusters_using_network_policies_ui_weekly
description: Weekly number of distinct clusters with network policies using the network policies UI
product_section: sec
product_stage: protect
product_group: group::container security
product_category: container_network_security
value_type: number
status: implemented
milestone: "14.1"
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64731
time_frame: 7d
data_source: redis_hll
data_category: Operational
distribution:
- ee
tier:
- ultimate
......@@ -9,7 +9,7 @@ RSpec.describe NetworkPolicies::ResourcesService do
let(:project) { create(:project) }
let(:cluster) { create(:cluster, :instance) }
let!(:cluster_kubernetes_namespace) { create(:cluster_kubernetes_namespace, project: project, cluster: cluster, environment: environment, namespace: 'namespace') }
let(:platform) { double('Clusters::Platforms::Kubernetes', kubeclient: kubeclient) }
let(:platform) { double('Clusters::Platforms::Kubernetes', kubeclient: kubeclient, cluster_id: cluster.id) }
let(:kubeclient) { double('Kubeclient::Client') }
let(:policy) do
Gitlab::Kubernetes::NetworkPolicy.new(
......@@ -46,6 +46,23 @@ RSpec.describe NetworkPolicies::ResourcesService do
expect(subject.payload.last.as_json).to eq(cilium_policy.as_json)
end
it_behaves_like 'tracking unique hll events' do
subject(:request) { service.execute }
let(:target_id) { 'clusters_using_network_policies_ui' }
let(:expected_type) { instance_of(Integer) }
before do
allow(kubeclient).to receive(:get_network_policies)
.with(namespace: cluster_kubernetes_namespace.namespace)
.and_return [policy.generate]
allow(kubeclient).to receive(:get_cilium_network_policies)
.with(namespace: cluster_kubernetes_namespace.namespace)
.and_return [cilium_policy.generate]
end
end
context 'without deployment_platform' do
let(:platform) { nil }
......
......@@ -369,3 +369,8 @@
category: testing
aggregation: weekly
feature_flag: users_expanding_widgets_usage_data
# Container Security - Network Policies
- name: clusters_using_network_policies_ui
redis_slot: network_policies
category: network_policies
aggregation: weekly
......@@ -114,7 +114,7 @@ namespace :gitlab do
end
desc 'Create missing dynamic database partitions'
task :create_dynamic_partitions do
task create_dynamic_partitions: :environment do
Gitlab::Database::Partitioning::PartitionCreator.new.create_partitions
end
......
......@@ -197,6 +197,52 @@ describe('Tracking', () => {
expectedError,
);
});
it('does not add empty form whitelist rules', () => {
Tracking.enableFormTracking({ fields: { allow: ['input-class1'] } });
expect(snowplowSpy).toHaveBeenCalledWith(
'enableFormTracking',
{ fields: { whitelist: ['input-class1'] } },
[],
);
});
describe('when `document.readyState` does not equal `complete`', () => {
const originalReadyState = document.readyState;
const setReadyState = (value) => {
Object.defineProperty(document, 'readyState', {
value,
configurable: true,
});
};
const fireReadyStateChangeEvent = () => {
document.dispatchEvent(new Event('readystatechange'));
};
beforeEach(() => {
setReadyState('interactive');
});
afterEach(() => {
setReadyState(originalReadyState);
});
it('does not call `window.snowplow` until `readystatechange` is fired and `document.readyState` equals `complete`', () => {
Tracking.enableFormTracking({ fields: { allow: ['input-class1'] } });
expect(snowplowSpy).not.toHaveBeenCalled();
fireReadyStateChangeEvent();
expect(snowplowSpy).not.toHaveBeenCalled();
setReadyState('complete');
fireReadyStateChangeEvent();
expect(snowplowSpy).toHaveBeenCalled();
});
});
});
describe('.flushPendingEvents', () => {
......
......@@ -46,7 +46,8 @@ RSpec.describe Gitlab::UsageDataCounters::HLLRedisCounter, :clean_gitlab_redis_s
'pipeline_authoring',
'epics_usage',
'epic_boards_usage',
'secure'
'secure',
'network_policies'
)
end
end
......
......@@ -1269,7 +1269,7 @@ RSpec.describe Gitlab::UsageData, :aggregate_failures do
let(:categories) { ::Gitlab::UsageDataCounters::HLLRedisCounter.categories }
let(:ineligible_total_categories) do
%w[source_code ci_secrets_management incident_management_alerts snippets terraform incident_management_oncall secure]
%w[source_code ci_secrets_management incident_management_alerts snippets terraform incident_management_oncall secure network_policies]
end
context 'with redis_hll_tracking feature enabled' do
......
......@@ -9,7 +9,7 @@ RSpec.describe Integrations::Ewm do
end
describe 'Validations' do
context 'when service is active' do
context 'when integration is active' do
before do
subject.active = true
end
......@@ -17,12 +17,12 @@ RSpec.describe Integrations::Ewm do
it { is_expected.to validate_presence_of(:project_url) }
it { is_expected.to validate_presence_of(:issues_url) }
it { is_expected.to validate_presence_of(:new_issue_url) }
it_behaves_like 'issue tracker service URL attribute', :project_url
it_behaves_like 'issue tracker service URL attribute', :issues_url
it_behaves_like 'issue tracker service URL attribute', :new_issue_url
it_behaves_like 'issue tracker integration URL attribute', :project_url
it_behaves_like 'issue tracker integration URL attribute', :issues_url
it_behaves_like 'issue tracker integration URL attribute', :new_issue_url
end
context 'when service is inactive' do
context 'when integration is inactive' do
before do
subject.active = false
end
......
This diff is collapsed.
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