30void WriteKey(
const std::string& key,
const std::string& file_name) {
31 printf(
"save key to path %s\n", file_name.c_str());
32 int fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
34 printf(
"open file %s fail %s\n", file_name.c_str(), strerror(errno));
38 write(fd, key.c_str(), key.size());
42KeyInfo
ReadKey(
const std::string& file_name) {
43 int fd = open(file_name.c_str(), O_RDONLY, 0666);
45 printf(
"open file %s fail %s\n", file_name.c_str(), strerror(errno));
53 read_len = read(fd, tmp,
sizeof(tmp));
57 res.append(tmp, read_len);
61 assert(key.ParseFromString(res));
75int main(
int argc,
char** argv) {
76 std::string path =
"./";
77 std::string admin_pri_key_path, admin_pub_key_path;
78 std::string node_pub_key_path;
85 "<save path> <administor private key path> <adminisotr public key "
86 "path> <node public key path> <node id> <ip> <port> "
87 "<type>(client/server)\n");
91 admin_pri_key_path = argv[2];
92 admin_pub_key_path = argv[3];
93 node_pub_key_path = argv[4];
94 node_id = strtoull(argv[5], NULL, 10);
96 port = strtoull(argv[7], NULL, 10);
100 KeyInfo pub_key =
ReadKey(node_pub_key_path);
103 KeyInfo admin_pri_key =
ReadKey(admin_pri_key_path);
104 KeyInfo admin_pub_key =
ReadKey(admin_pub_key_path);
106 CertificateInfo info;
107 info.mutable_admin_public_key()->set_key(admin_pub_key.key());
108 info.mutable_admin_public_key()->set_hash_type(admin_pub_key.hash_type());
109 *info.mutable_public_key()->mutable_public_key_info()->mutable_key() =
111 info.mutable_public_key()->mutable_public_key_info()->set_node_id(node_id);
112 if (
"client" == type) {
113 info.mutable_public_key()->mutable_public_key_info()->set_type(
114 CertificateKeyInfo::CLIENT);
116 info.mutable_public_key()->mutable_public_key_info()->set_type(
117 CertificateKeyInfo::REPLICA);
119 info.mutable_public_key()->mutable_public_key_info()->set_ip(ip);
120 info.mutable_public_key()->mutable_public_key_info()->set_port(port);
122 info.set_node_id(node_id);
130 assert(resp_info.ok());
131 *info.mutable_public_key()->mutable_certificate() = *resp_info;
132 std::cout <<
"info:" << info.DebugString();
135 assert(info.SerializeToString(&str));
136 WriteKey(str, path +
"/cert_" + std::to_string(node_id) +
".cert");