blob: 628483a3750896f1f05ce2ee40ad5498ec2366be (
plain)
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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_BUILDER_H_
#define CHROME_BROWSER_POLICY_CLOUD_POLICY_BUILDER_H_
#include <string>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/policy/proto/cloud/device_management_local.pb.h"
#include "crypto/rsa_private_key.h"
namespace enterprise_management {
class CloudPolicySettings;
class ExternalPolicyData;
} // namespace enterprise_management
namespace policy {
// A helper class for testing that provides a straightforward interface for
// constructing policy blobs for use in testing. NB: This uses fake data and
// hard-coded signing keys by default, so should not be used in production code.
class PolicyBuilder {
public:
// Constants used as dummy data for filling the PolicyData protobuf.
static const char kFakeDeviceId[];
static const char kFakeDomain[];
static const char kFakeMachineName[];
static const char kFakePolicyType[];
static const int kFakePublicKeyVersion;
static const int64 kFakeTimestamp;
static const char kFakeToken[];
static const char kFakeUsername[];
// Creates a policy builder. The builder will have all PolicyData fields
// initialized to dummy values and use the test signing keys.
PolicyBuilder();
virtual ~PolicyBuilder();
// Use this member to access the PolicyData protobuf.
enterprise_management::PolicyData& policy_data() {
if (!policy_data_.get())
policy_data_.reset(new enterprise_management::PolicyData());
return *policy_data_;
}
void clear_policy_data() {
policy_data_.reset();
}
enterprise_management::PolicyFetchResponse& policy() {
return policy_;
}
crypto::RSAPrivateKey* signing_key() {
return signing_key_.get();
}
void set_signing_key(scoped_ptr<crypto::RSAPrivateKey> signing_key) {
signing_key_ = signing_key.Pass();
}
crypto::RSAPrivateKey* new_signing_key() {
return new_signing_key_.get();
}
void set_new_signing_key(scoped_ptr<crypto::RSAPrivateKey> new_signing_key) {
new_signing_key_ = new_signing_key.Pass();
}
// Assembles the policy components. The resulting policy protobuf is available
// through policy() after this call.
virtual void Build();
// Returns a copy of policy().
scoped_ptr<enterprise_management::PolicyFetchResponse> GetCopy();
// Returns a binary policy blob, i.e. an encoded PolicyFetchResponse.
std::string GetBlob();
// These return hard-coded testing keys. Don't use in production!
static scoped_ptr<crypto::RSAPrivateKey> CreateTestSigningKey();
static scoped_ptr<crypto::RSAPrivateKey> CreateTestNewSigningKey();
private:
// Produces |key|'s signature over |data| and stores it in |signature|.
void SignData(const std::string& data,
crypto::RSAPrivateKey* key,
std::string* signature);
enterprise_management::PolicyFetchResponse policy_;
scoped_ptr<enterprise_management::PolicyData> policy_data_;
std::string payload_data_;
scoped_ptr<crypto::RSAPrivateKey> signing_key_;
scoped_ptr<crypto::RSAPrivateKey> new_signing_key_;
DISALLOW_COPY_AND_ASSIGN(PolicyBuilder);
};
// Type-parameterized PolicyBuilder extension that allows for building policy
// blobs carrying protobuf payloads.
template<typename PayloadProto>
class TypedPolicyBuilder : public PolicyBuilder {
public:
TypedPolicyBuilder();
virtual ~TypedPolicyBuilder() {}
// Returns a reference to the payload protobuf being built.
PayloadProto& payload() {
if (!payload_.get())
payload_.reset(new PayloadProto());
return *payload_;
}
void clear_payload() {
payload_.reset();
}
// PolicyBuilder:
virtual void Build() OVERRIDE {
if (payload_.get())
CHECK(payload_->SerializeToString(policy_data().mutable_policy_value()));
PolicyBuilder::Build();
}
private:
scoped_ptr<PayloadProto> payload_;
DISALLOW_COPY_AND_ASSIGN(TypedPolicyBuilder);
};
typedef TypedPolicyBuilder<enterprise_management::CloudPolicySettings>
UserPolicyBuilder;
typedef TypedPolicyBuilder<enterprise_management::ExternalPolicyData>
ComponentPolicyBuilder;
} // namespace policy
#endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_BUILDER_H_
|