1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "host/commands/cvd/parser/instance/cf_security_configs.h"
17
18 #include <android-base/logging.h>
19
20 #include "host/commands/assemble_cvd/flags_defaults.h"
21 #include "host/commands/cvd/parser/cf_configs_common.h"
22 #include "host/libs/config/cuttlefish_config.h"
23
24 namespace cuttlefish {
25
26 /*This function is created to cover the initiation use_random_serial flag
27 when the json value of serial_number equal "@random"
28 */
InitRandomSerialNumber(Json::Value & instances)29 void InitRandomSerialNumber(Json::Value& instances) {
30 int size = instances.size();
31 for (int i = 0; i < size; i++) {
32 std::string serial_number_str =
33 instances[i]["security"]["serial_number"].asString();
34 if (serial_number_str == "@random") {
35 instances[i]["security"]["use_random_serial"] = true;
36 } else {
37 instances[i]["security"]["use_random_serial"] = false;
38 }
39 }
40 }
41
InitSecurityConfigs(Json::Value & instances)42 void InitSecurityConfigs(Json::Value& instances) {
43 InitStringConfig(instances, "security", "serial_number",
44 CF_DEFAULTS_SERIAL_NUMBER);
45 // This init should be called after the InitSecurityConfigs call, since it
46 // depends on serial_number flag
47 InitRandomSerialNumber(instances);
48 InitBoolConfig(instances, "security", "guest_enforce_security",
49 CF_DEFAULTS_GUEST_ENFORCE_SECURITY);
50 }
51
GenerateSecurityFlags(const Json::Value & instances)52 std::vector<std::string> GenerateSecurityFlags(const Json::Value& instances) {
53 std::vector<std::string> result;
54 if (!GENERATE_MVP_FLAGS_ONLY) {
55 result.emplace_back(
56 GenerateGflag(instances, "serial_number", "security", "serial_number"));
57 result.emplace_back(GenerateGflag(instances, "use_random_serial",
58 "security", "use_random_serial"));
59 }
60 result.emplace_back(GenerateGflag(instances, "guest_enforce_security",
61 "security", "guest_enforce_security"));
62 return result;
63 }
64
65 } // namespace cuttlefish
66