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