]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_token.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rgw / rgw_token.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 Red Hat, Inc.
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include <errno.h>
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19
20 #include "common/config.h"
21 #include "common/ceph_argparse.h"
22 #include "common/debug.h"
23 #include "global/global_init.h"
24 #include "include/assert.h"
25 #include "include/str_list.h"
26
27 #include "rgw_token.h"
28 #include "rgw_b64.h"
29
30 #define dout_subsys ceph_subsys_rgw
31
32 namespace {
33
34 using namespace rgw;
35 using std::get;
36 using std::string;
37
38 RGWToken::token_type type{RGWToken::TOKEN_NONE};
39 string access_key{""};
40 string secret_key{""};
41
42 Formatter* formatter{nullptr};
43
44 bool verbose {false};
45 bool do_encode {false};
46 bool do_decode {false};
47
48 }
49
50 void usage()
51 {
52 cout << "usage: radosgw-token --encode --ttype=<token type> [options...]" << std::endl;
53 cout << "\t(maybe exporting RGW_ACCESS_KEY_ID and RGW_SECRET_ACCESS_KEY)"
54 << std::endl;
55 cout << "\t <token type> := ad | ldap" << std::endl;
56 cout << "\n";
57 generic_client_usage();
58 }
59
60 int main(int argc, char **argv)
61 {
62 std::string val;
63 vector<const char*> args;
64 argv_to_vec(argc, (const char **)argv, args);
65 env_to_vec(args);
66
67 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
68 CODE_ENVIRONMENT_UTILITY, 0);
69 common_init_finish(g_ceph_context);
70
71 char *v{nullptr};
72 v = getenv("RGW_ACCESS_KEY_ID");
73 if (v) {
74 access_key = v;
75 }
76
77 v = getenv("RGW_SECRET_ACCESS_KEY");
78 if (v) {
79 secret_key = v;
80 }
81
82 for (auto arg_iter = args.begin(); arg_iter != args.end();) {
83 if (ceph_argparse_witharg(args, arg_iter, &val, "--access",
84 (char*) nullptr)) {
85 access_key = val;
86 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--secret",
87 (char*) nullptr)) {
88 secret_key = val;
89 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--ttype",
90 (char*) nullptr)) {
91 for (const auto& ttype : {"ad", "ldap"}) {
92 if (boost::iequals(val, ttype)) {
93 type = RGWToken::to_type(val);
94 break;
95 }
96 }
97 } else if (ceph_argparse_flag(args, arg_iter, "--encode",
98 (char*) nullptr)) {
99 do_encode = true;
100 } else if (ceph_argparse_flag(args, arg_iter, "--decode",
101 (char*) nullptr)) {
102 do_decode = true;
103 } else if (ceph_argparse_flag(args, arg_iter, "--verbose",
104 (char*) nullptr)) {
105 verbose = true;
106 } else {
107 ++arg_iter;
108 }
109 }
110
111 if ((! do_encode) ||
112 (type == RGWToken::TOKEN_NONE)) {
113 usage();
114 return -EINVAL;
115 }
116
117 formatter = new JSONFormatter(true /* pretty */);
118
119 RGWToken token(type, access_key, secret_key);
120 if (do_encode) {
121 token.encode_json(formatter);
122 std::ostringstream os;
123 formatter->flush(os);
124 string token_str = os.str();
125 if (verbose) {
126 std::cout << "expanded token: " << token_str << std::endl;
127 if (do_decode) {
128 RGWToken token2(token_str);
129 std::cout << "decoded expanded token: " << token2 << std::endl;
130 }
131 }
132 std::cout << to_base64(token_str) << std::endl;
133 }
134
135 return 0;
136 }