update_engine: Add --properties_format flag to delta_generator
We need to be able to capture information about a payload by just
looking at it. These information needed for nebraska to be able to
process a response from a request. These information includes:
- Payload and its metadata hashes and sizes.
- Payload metadata signature.
- The APP ID of the original image.
- Whether the payload is a delta or full.
- The payload's target version.
This CL adds the ability to generate a json file with the payloads
properties such as above.
Also this CL refactors how this information is generated into a single
class.
BUG=chromium:960433
TEST=delta_generator --in_file=hello-signed-delta --properties_file=prop
TEST=delta_generator --in_file=hello-signed-delta
--properties_file=payload.json --properties_format="json"
Change-Id: Ia61be0bf37bcacfd82f8982a7977fdae2f18cb30
Reviewed-on: https://chromium-review.googlesource.com/1610801
Tested-by: Amin Hassani <ahassani@chromium.org>
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Sen Jiang <senj@chromium.org>
Reviewed-by: Nicolas Norvez <norvez@chromium.org>
diff --git a/payload_generator/generate_delta_main.cc b/payload_generator/generate_delta_main.cc
index de0a091..10ae2a0 100644
--- a/payload_generator/generate_delta_main.cc
+++ b/payload_generator/generate_delta_main.cc
@@ -38,6 +38,7 @@
#include "update_engine/payload_consumer/payload_constants.h"
#include "update_engine/payload_generator/delta_diff_generator.h"
#include "update_engine/payload_generator/payload_generation_config.h"
+#include "update_engine/payload_generator/payload_properties.h"
#include "update_engine/payload_generator/payload_signer.h"
#include "update_engine/payload_generator/xz.h"
#include "update_engine/update_metadata.pb.h"
@@ -53,6 +54,9 @@
namespace {
+constexpr char kPayloadPropertiesFormatKeyValue[] = "key-value";
+constexpr char kPayloadPropertiesFormatJson[] = "json";
+
void ParseSignatureSizes(const string& signature_sizes_flag,
vector<int>* signature_sizes) {
signature_sizes->clear();
@@ -268,14 +272,24 @@
return true;
}
-int ExtractProperties(const string& payload_path, const string& props_file) {
- brillo::KeyValueStore properties;
- TEST_AND_RETURN_FALSE(
- PayloadSigner::ExtractPayloadProperties(payload_path, &properties));
- if (props_file == "-") {
- printf("%s", properties.SaveToString().c_str());
+bool ExtractProperties(const string& payload_path,
+ const string& props_file,
+ const string& props_format) {
+ string properties;
+ PayloadProperties payload_props(payload_path);
+ if (props_format == kPayloadPropertiesFormatKeyValue) {
+ TEST_AND_RETURN_FALSE(payload_props.GetPropertiesAsKeyValue(&properties));
+ } else if (props_format == kPayloadPropertiesFormatJson) {
+ TEST_AND_RETURN_FALSE(payload_props.GetPropertiesAsJson(&properties));
} else {
- properties.Save(base::FilePath(props_file));
+ LOG(FATAL) << "Invalid option " << props_format
+ << " for --properties_format flag.";
+ }
+ if (props_file == "-") {
+ printf("%s", properties.c_str());
+ } else {
+ utils::WriteFile(
+ props_file.c_str(), properties.c_str(), properties.length());
LOG(INFO) << "Generated properties file at " << props_file;
}
return true;
@@ -362,7 +376,11 @@
DEFINE_string(properties_file,
"",
"If passed, dumps the payload properties of the payload passed "
- "in --in_file and exits.");
+ "in --in_file and exits. Look at --properties_format.");
+ DEFINE_string(properties_format,
+ kPayloadPropertiesFormatKeyValue,
+ "Defines the format of the --properties_file. The acceptable "
+ "values are: key-value (default) and json");
DEFINE_int64(max_timestamp,
0,
"The maximum timestamp of the OS allowed to apply this "
@@ -467,7 +485,10 @@
return VerifySignedPayload(FLAGS_in_file, FLAGS_public_key);
}
if (!FLAGS_properties_file.empty()) {
- return ExtractProperties(FLAGS_in_file, FLAGS_properties_file) ? 0 : 1;
+ return ExtractProperties(
+ FLAGS_in_file, FLAGS_properties_file, FLAGS_properties_format)
+ ? 0
+ : 1;
}
// A payload generation was requested. Convert the flags to a
diff --git a/payload_generator/payload_properties.cc b/payload_generator/payload_properties.cc
new file mode 100644
index 0000000..53e69f3
--- /dev/null
+++ b/payload_generator/payload_properties.cc
@@ -0,0 +1,143 @@
+//
+// Copyright 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/payload_generator/payload_properties.h"
+
+#include <algorithm>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <base/json/json_writer.h>
+#include <base/strings/string_util.h>
+#include <base/values.h>
+#include <brillo/data_encoding.h>
+
+#include "update_engine/common/constants.h"
+#include "update_engine/common/hash_calculator.h"
+#include "update_engine/common/utils.h"
+#include "update_engine/payload_consumer/payload_metadata.h"
+#include "update_engine/update_metadata.pb.h"
+
+using std::string;
+using std::vector;
+
+namespace chromeos_update_engine {
+
+namespace {
+// These ones are needed by the GoldenEye.
+const char kPayloadPropertyJsonVersion[] = "version";
+const char kPayloadPropertyJsonPayloadHash[] = "sha256_hex";
+const char kPayloadPropertyJsonMetadataSize[] = "metadata_size";
+const char kPayloadPropertyJsonMetadataSignature[] = "metadata_signature";
+
+// These are needed by the Nebraska and devserver.
+const char kPayloadPropertyJsonPayloadSize[] = "size";
+const char kPayloadPropertyJsonIsDelta[] = "is_delta";
+const char kPayloadPropertyJsonTargetVersion[] = "target_version";
+const char kPayloadPropertyJsonSourceVersion[] = "source_version";
+} // namespace
+
+PayloadProperties::PayloadProperties(const string& payload_path)
+ : payload_path_(payload_path) {}
+
+bool PayloadProperties::GetPropertiesAsJson(string* json_str) {
+ TEST_AND_RETURN_FALSE(LoadFromPayload());
+
+ base::DictionaryValue properties;
+ properties.SetInteger(kPayloadPropertyJsonVersion, version_);
+ properties.SetInteger(kPayloadPropertyJsonMetadataSize, metadata_size_);
+ properties.SetString(kPayloadPropertyJsonMetadataSignature,
+ metadata_signatures_);
+ properties.SetInteger(kPayloadPropertyJsonPayloadSize, payload_size_);
+ properties.SetString(kPayloadPropertyJsonPayloadHash, payload_hash_);
+ properties.SetBoolean(kPayloadPropertyJsonIsDelta, is_delta_);
+ properties.SetString(kPayloadPropertyJsonTargetVersion, target_version_);
+ if (is_delta_) {
+ properties.SetString(kPayloadPropertyJsonSourceVersion, source_version_);
+ }
+
+ return base::JSONWriter::Write(properties, json_str);
+}
+
+bool PayloadProperties::GetPropertiesAsKeyValue(string* key_value_str) {
+ TEST_AND_RETURN_FALSE(LoadFromPayload());
+
+ brillo::KeyValueStore properties;
+ properties.SetString(kPayloadPropertyFileSize, std::to_string(payload_size_));
+ properties.SetString(kPayloadPropertyMetadataSize,
+ std::to_string(metadata_size_));
+ properties.SetString(kPayloadPropertyFileHash, payload_hash_);
+ properties.SetString(kPayloadPropertyMetadataHash, metadata_hash_);
+
+ *key_value_str = properties.SaveToString();
+ return true;
+}
+
+bool PayloadProperties::LoadFromPayload() {
+ PayloadMetadata payload_metadata;
+ DeltaArchiveManifest manifest;
+ Signatures metadata_signatures;
+ TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadFile(
+ payload_path_, &manifest, &metadata_signatures));
+
+ metadata_size_ = payload_metadata.GetMetadataSize();
+ payload_size_ = utils::FileSize(payload_path_);
+
+ brillo::Blob metadata_hash;
+ TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
+ payload_path_, metadata_size_, &metadata_hash) ==
+ static_cast<off_t>(metadata_size_));
+ metadata_hash_ = brillo::data_encoding::Base64Encode(metadata_hash);
+
+ brillo::Blob payload_hash;
+ TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
+ payload_path_, payload_size_, &payload_hash) ==
+ static_cast<off_t>(payload_size_));
+ payload_hash_ = brillo::data_encoding::Base64Encode(payload_hash);
+
+ if (payload_metadata.GetMetadataSignatureSize() > 0) {
+ TEST_AND_RETURN_FALSE(metadata_signatures.signatures_size() > 0);
+ vector<string> base64_signatures;
+ for (const auto& sig : metadata_signatures.signatures()) {
+ base64_signatures.push_back(
+ brillo::data_encoding::Base64Encode(sig.data()));
+ }
+ metadata_signatures_ = base::JoinString(base64_signatures, ":");
+ }
+
+ is_delta_ = manifest.has_old_image_info() || manifest.has_old_kernel_info() ||
+ manifest.has_old_rootfs_info() ||
+ std::any_of(manifest.partitions().begin(),
+ manifest.partitions().end(),
+ [](const PartitionUpdate& part) {
+ return part.has_old_partition_info();
+ });
+
+ if (manifest.has_new_image_info()) {
+ target_version_ = manifest.new_image_info().version();
+ } else {
+ target_version_ = "99999.0.0";
+ }
+
+ // No need to set the source version if it was not a delta payload.
+ if (is_delta_ && manifest.has_old_image_info()) {
+ source_version_ = manifest.old_image_info().version();
+ }
+ return true;
+}
+
+} // namespace chromeos_update_engine
diff --git a/payload_generator/payload_properties.h b/payload_generator/payload_properties.h
new file mode 100644
index 0000000..3b34511
--- /dev/null
+++ b/payload_generator/payload_properties.h
@@ -0,0 +1,73 @@
+//
+// Copyright 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_PROPERTIES_H_
+#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_PROPERTIES_H_
+
+#include <string>
+
+#include <brillo/key_value_store.h>
+#include <brillo/secure_blob.h>
+
+namespace chromeos_update_engine {
+
+// A class for extracting information about a payload from the payload file
+// itself. Currently the metadata can be exported as a json file or a key/value
+// properties file. But more can be added if required.
+class PayloadProperties {
+ public:
+ explicit PayloadProperties(const std::string& payload_path);
+ ~PayloadProperties() = default;
+
+ // Get the properties in a json format. The json file will be used in
+ // autotests, cros flash, etc. Mainly in Chrome OS.
+ bool GetPropertiesAsJson(std::string* json_str);
+
+ // Get the properties of the payload as a key/value store. This is mainly used
+ // in Android.
+ bool GetPropertiesAsKeyValue(std::string* key_value_str);
+
+ private:
+ // Does the main job of reading the payload and extracting information from
+ // it.
+ bool LoadFromPayload();
+
+ // The path to the payload file.
+ std::string payload_path_;
+
+ // The version of the metadata json format. If the output json file changes
+ // format, this needs to be increased.
+ int version_{2};
+
+ size_t metadata_size_;
+ std::string metadata_hash_;
+ std::string metadata_signatures_;
+
+ size_t payload_size_;
+ std::string payload_hash_;
+
+ // Whether the payload is a delta (true) or full (false).
+ bool is_delta_;
+
+ std::string target_version_;
+ std::string source_version_;
+
+ DISALLOW_COPY_AND_ASSIGN(PayloadProperties);
+};
+
+} // namespace chromeos_update_engine
+
+#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_PROPERTIES_H_
diff --git a/payload_generator/payload_signer.cc b/payload_generator/payload_signer.cc
index 2d0489a..2a7021f 100644
--- a/payload_generator/payload_signer.cc
+++ b/payload_generator/payload_signer.cc
@@ -439,35 +439,4 @@
return true;
}
-bool PayloadSigner::ExtractPayloadProperties(
- const string& payload_path, brillo::KeyValueStore* properties) {
- brillo::Blob payload;
- TEST_AND_RETURN_FALSE(
- utils::ReadFileChunk(payload_path, 0, kMaxPayloadHeaderSize, &payload));
-
- PayloadMetadata payload_metadata;
- TEST_AND_RETURN_FALSE(payload_metadata.ParsePayloadHeader(payload));
- uint64_t metadata_size = payload_metadata.GetMetadataSize();
-
- uint64_t file_size = utils::FileSize(payload_path);
- properties->SetString(kPayloadPropertyFileSize, std::to_string(file_size));
- properties->SetString(kPayloadPropertyMetadataSize,
- std::to_string(metadata_size));
-
- brillo::Blob file_hash, metadata_hash;
- TEST_AND_RETURN_FALSE(
- HashCalculator::RawHashOfFile(payload_path, file_size, &file_hash) ==
- static_cast<off_t>(file_size));
-
- TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfFile(
- payload_path, metadata_size, &metadata_hash) ==
- static_cast<off_t>(metadata_size));
-
- properties->SetString(kPayloadPropertyFileHash,
- brillo::data_encoding::Base64Encode(file_hash));
- properties->SetString(kPayloadPropertyMetadataHash,
- brillo::data_encoding::Base64Encode(metadata_hash));
- return true;
-}
-
} // namespace chromeos_update_engine
diff --git a/payload_generator/payload_signer.h b/payload_generator/payload_signer.h
index b2d6606..83ddadc 100644
--- a/payload_generator/payload_signer.h
+++ b/payload_generator/payload_signer.h
@@ -119,9 +119,6 @@
const std::string& private_key_path,
std::string* out_signature);
- static bool ExtractPayloadProperties(const std::string& payload_path,
- brillo::KeyValueStore* properties);
-
private:
// This should never be constructed
DISALLOW_IMPLICIT_CONSTRUCTORS(PayloadSigner);