File: profile_policies.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (323 lines) | stat: -rw-r--r-- 11,782 bytes parent folder | download | duplicates (7)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/profile_policies.h"

#include <iterator>
#include <optional>
#include <string>
#include <utility>

#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/values.h"
#include "chromeos/ash/components/network/client_cert_util.h"
#include "chromeos/ash/components/network/policy_util.h"
#include "chromeos/components/onc/onc_signature.h"
#include "chromeos/components/onc/onc_utils.h"
#include "chromeos/components/onc/variable_expander.h"
#include "components/device_event_log/device_event_log.h"
#include "components/onc/onc_constants.h"

namespace ash {

namespace {

bool DefaultShillPropertiesMatcher(
    const base::Value::Dict& onc_network_configuration,
    const base::Value::Dict& shill_properties) {
  return policy_util::IsPolicyMatching(onc_network_configuration,
                                       shill_properties);
}

base::flat_map<std::string, std::string> GetAllExpansions(
    const base::flat_map<std::string, std::string>& profile_wide_expansions,
    const client_cert::ResolvedCert& resolved_cert) {
  base::flat_map<std::string, std::string> result;
  result.insert(profile_wide_expansions.begin(), profile_wide_expansions.end());
  if (resolved_cert.status() ==
      client_cert::ResolvedCert::Status::kCertMatched) {
    result.insert(resolved_cert.variable_expansions().begin(),
                  resolved_cert.variable_expansions().end());
  }
  return result;
}

base::Value::Dict DefaultRuntimeValuesSetter(
    const base::Value::Dict& onc_network_configuration,
    const base::flat_map<std::string, std::string>& profile_wide_expansions,
    const client_cert::ResolvedCert& resolved_cert) {
  // TODO(b/215163180): Change this to return a nullopt or the like instead of
  // cloning if the variable expansion doesn't change anything when this is the
  // only caller of ExpandStringsInOncObject.
  base::Value::Dict expanded = onc_network_configuration.Clone();
  chromeos::VariableExpander variable_expander(
      GetAllExpansions(profile_wide_expansions, resolved_cert));
  chromeos::onc::ExpandStringsInOncObject(
      chromeos::onc::kNetworkConfigurationSignature, variable_expander,
      &expanded);
  client_cert::SetResolvedCertInOnc(resolved_cert, expanded);
  return expanded;
}

}  // namespace

ProfilePolicies::NetworkPolicy::NetworkPolicy(const ProfilePolicies* parent,
                                              base::Value::Dict onc_policy)
    : parent_(parent), original_policy_(std::move(onc_policy)) {
  // There could already be profile-wide variable expansions (through parent_).
  ReapplyVariableExpansions();
  ReapplyRuntimeValues();
}

ProfilePolicies::NetworkPolicy::~NetworkPolicy() = default;

ProfilePolicies::NetworkPolicy::NetworkPolicy(NetworkPolicy&& other) = default;
ProfilePolicies::NetworkPolicy& ProfilePolicies::NetworkPolicy::operator=(
    NetworkPolicy&& other) = default;

ProfilePolicies::ChangeEffect ProfilePolicies::NetworkPolicy::UpdateFrom(
    const base::Value::Dict& new_onc_policy) {
  if (new_onc_policy == original_policy_)
    return ChangeEffect::kNoChange;
  original_policy_ = new_onc_policy.Clone();
  ReapplyVariableExpansions();
  ReapplyRuntimeValues();
  return ChangeEffect::kEffectivePolicyChanged;
}

ProfilePolicies::ProfilePolicies()
    : shill_properties_matcher_(
          base::BindRepeating(&DefaultShillPropertiesMatcher)),
      runtime_values_setter_(base::BindRepeating(&DefaultRuntimeValuesSetter)) {
}
ProfilePolicies::~ProfilePolicies() = default;

ProfilePolicies::ChangeEffect
ProfilePolicies::NetworkPolicy::SetResolvedClientCertificate(
    client_cert::ResolvedCert resolved_cert) {
  if (resolved_cert_ == resolved_cert)
    return ChangeEffect::kNoChange;
  resolved_cert_ = std::move(resolved_cert);
  return ReapplyRuntimeValues();
}

ProfilePolicies::ChangeEffect
ProfilePolicies::NetworkPolicy::OnProfileWideExpansionsChanged() {
  ProfilePolicies::ChangeEffect change_effect = ChangeEffect::kNoChange;
  if (ReapplyVariableExpansions() == ChangeEffect::kEffectivePolicyChanged) {
    change_effect = ChangeEffect::kEffectivePolicyChanged;
  }
  if (ReapplyRuntimeValues() == ChangeEffect::kEffectivePolicyChanged) {
    change_effect = ChangeEffect::kEffectivePolicyChanged;
  }
  return change_effect;
}

const base::Value::Dict& ProfilePolicies::NetworkPolicy::GetOriginalPolicy()
    const {
  return original_policy_;
}

const base::Value::Dict&
ProfilePolicies::NetworkPolicy::GetPolicyWithVariablesExpanded() const {
  if (!policy_with_placeholders_replaced_.has_value()) {
    // Memory optimization to avoid storing the same value twice if setting
    // runtime values resulted in no change.
    return original_policy_;
  }
  return policy_with_placeholders_replaced_.value();
}

const base::Value::Dict&
ProfilePolicies::NetworkPolicy::GetPolicyWithRuntimeValues() const {
  if (!policy_with_runtime_values_.has_value()) {
    // Memory optimization to avoid storing the same value twice if setting
    // runtime values resulted in no change.
    return original_policy_;
  }
  return policy_with_runtime_values_.value();
}

ProfilePolicies::ChangeEffect
ProfilePolicies::NetworkPolicy::ReapplyVariableExpansions() {
  std::optional<base::Value::Dict> old_policy_with_placeholders_replaced =
      std::move(policy_with_placeholders_replaced_);

  policy_with_placeholders_replaced_ = parent_->runtime_values_setter_.Run(
      original_policy_, parent_->profile_wide_expansions_,
      client_cert::ResolvedCert::NotKnownYet());
  if (policy_with_placeholders_replaced_ == original_policy_) {
    // Memory optimization to avoid storing the same value twice if variable
    // expansion had no effect.
    policy_with_placeholders_replaced_.reset();
  }

  return old_policy_with_placeholders_replaced ==
                 policy_with_placeholders_replaced_
             ? ChangeEffect::kNoChange
             : ChangeEffect::kEffectivePolicyChanged;
}

ProfilePolicies::ChangeEffect
ProfilePolicies::NetworkPolicy::ReapplyRuntimeValues() {
  std::optional<base::Value::Dict> old_policy_with_runtime_values =
      std::move(policy_with_runtime_values_);

  policy_with_runtime_values_ = parent_->runtime_values_setter_.Run(
      original_policy_, parent_->profile_wide_expansions_, resolved_cert_);
  if (policy_with_runtime_values_ == original_policy_) {
    // Memory optimization to avoid storing the same value twice if variable
    // expansion had no effect.
    policy_with_runtime_values_.reset();
  }

  return old_policy_with_runtime_values == policy_with_runtime_values_
             ? ChangeEffect::kNoChange
             : ChangeEffect::kEffectivePolicyChanged;
}

base::flat_set<std::string> ProfilePolicies::ApplyOncNetworkConfigurationList(
    const base::Value::List& network_configs_onc) {
  base::flat_set<std::string> processed_guids;
  base::flat_set<std::string> new_or_modified_guids;
  base::flat_set<std::string> removed_guids = GetAllPolicyGuids();

  for (const base::Value& network_value : network_configs_onc) {
    const base::Value::Dict& network = network_value.GetDict();

    const std::string* guid_str =
        network.FindString(::onc::network_config::kGUID);
    DCHECK(guid_str && !guid_str->empty());
    std::string guid = *guid_str;
    if (base::Contains(processed_guids, guid)) {
      NET_LOG(ERROR) << "ONC Contains multiple entries for the same guid: "
                     << guid;
      continue;
    }
    processed_guids.insert(guid);

    NetworkPolicy* existing_policy = FindPolicy(guid);
    if (!existing_policy) {
      guid_to_policy_.insert(
          std::make_pair(guid, NetworkPolicy(this, network.Clone())));
      new_or_modified_guids.insert(guid);
      continue;
    }
    removed_guids.erase(guid);
    if (existing_policy->UpdateFrom(network) ==
        ChangeEffect::kEffectivePolicyChanged) {
      new_or_modified_guids.insert(guid);
    }
  }

  for (const std::string& removed_guid : removed_guids) {
    guid_to_policy_.erase(removed_guid);
  }

  return new_or_modified_guids;
}

void ProfilePolicies::SetGlobalNetworkConfig(
    const base::Value::Dict& global_network_config) {
  global_network_config_ = global_network_config.Clone();
}

base::flat_set<std::string> ProfilePolicies::SetProfileWideExpansions(
    base::flat_map<std::string, std::string> expansions) {
  if (profile_wide_expansions_ == expansions)
    return {};
  profile_wide_expansions_ = std::move(expansions);
  base::flat_set<std::string> modified_guids;
  for (auto& pair : guid_to_policy_) {
    if (pair.second.OnProfileWideExpansionsChanged() ==
        ChangeEffect::kEffectivePolicyChanged) {
      modified_guids.insert(pair.first);
    }
  }
  return modified_guids;
}

bool ProfilePolicies::SetResolvedClientCertificate(
    const std::string& guid,
    client_cert::ResolvedCert resolved_cert) {
  base::flat_set<std::string> modified_guids;
  NetworkPolicy* policy = FindPolicy(guid);
  if (!policy)
    return false;
  return policy->SetResolvedClientCertificate(std::move(resolved_cert)) ==
         ChangeEffect::kEffectivePolicyChanged;
}

const base::Value::Dict* ProfilePolicies::GetPolicyByGuid(
    const std::string& guid) const {
  const NetworkPolicy* policy = FindPolicy(guid);
  return policy ? &policy->GetPolicyWithRuntimeValues() : nullptr;
}

const base::Value::Dict* ProfilePolicies::GetPolicyWithVariablesExpandedByGuid(
    const std::string& guid) const {
  const NetworkPolicy* policy = FindPolicy(guid);
  return policy ? &policy->GetPolicyWithVariablesExpanded() : nullptr;
}

const base::Value::Dict* ProfilePolicies::GetOriginalPolicyByGuid(
    const std::string& guid) const {
  const NetworkPolicy* policy = FindPolicy(guid);
  return policy ? &policy->GetOriginalPolicy() : nullptr;
}

bool ProfilePolicies::HasPolicyMatchingShillProperties(
    const base::Value::Dict& shill_properties) const {
  for (const auto& [guid, policy] : guid_to_policy_) {
    if (shill_properties_matcher_.Run(policy.GetPolicyWithRuntimeValues(),
                                      shill_properties)) {
      return true;
    }
  }
  return false;
}

base::flat_map<std::string, base::Value::Dict>
ProfilePolicies::GetGuidToPolicyMap() const {
  std::vector<std::pair<std::string, base::Value::Dict>> result;
  result.reserve(guid_to_policy_.size());
  for (const auto& [guid, policy] : guid_to_policy_) {
    result.emplace_back(guid, policy.GetPolicyWithRuntimeValues().Clone());
  }
  return base::flat_map<std::string, base::Value::Dict>(std::move(result));
}

void ProfilePolicies::SetShillPropertiesMatcherForTesting(
    const ShillPropertiesMatcher& shill_properties_matcher) {
  shill_properties_matcher_ = shill_properties_matcher;
}

void ProfilePolicies::SetRuntimeValuesSetterForTesting(
    const RuntimeValuesSetter& runtime_values_setter) {
  runtime_values_setter_ = runtime_values_setter;
}

base::flat_set<std::string> ProfilePolicies::GetAllPolicyGuids() const {
  std::vector<std::string> result;
  result.reserve(guid_to_policy_.size());
  for (const auto& [guid, _] : guid_to_policy_) {
    result.push_back(guid);
  }
  return base::flat_set<std::string>(result);
}

ProfilePolicies::NetworkPolicy* ProfilePolicies::FindPolicy(
    const std::string& guid) {
  auto iter = guid_to_policy_.find(guid);
  return iter != guid_to_policy_.end() ? &(iter->second) : nullptr;
}

const ProfilePolicies::NetworkPolicy* ProfilePolicies::FindPolicy(
    const std::string& guid) const {
  auto iter = guid_to_policy_.find(guid);
  return iter != guid_to_policy_.end() ? &(iter->second) : nullptr;
}

}  // namespace ash