summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_token.cc
blob: 00430a6096a360dca4004b9fa5c8d99292c14b9f (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
142
143
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include <errno.h>
#include <iostream>
#include <sstream>
#include <string>

#include "common/config.h"
#include "common/ceph_argparse.h"
#include "common/debug.h"
#include "global/global_init.h"
#include "include/ceph_assert.h"
#include "include/str_list.h"

#include "rgw_token.h"
#include "rgw_b64.h"

#define dout_subsys ceph_subsys_rgw

namespace {

  using namespace rgw;
  using std::get;
  using std::string;

  RGWToken::token_type type{RGWToken::TOKEN_NONE};
  string access_key{""};
  string secret_key{""};

  Formatter* formatter{nullptr};

  bool verbose {false};
  bool do_encode {false};
  bool do_decode {false};

}

void usage()
{
  cout << "usage: radosgw-token --encode --ttype=<token type> [options...]" << std::endl;
  cout << "\t(maybe exporting RGW_ACCESS_KEY_ID and RGW_SECRET_ACCESS_KEY)"    
       << std::endl;
  cout << "\t <token type> := ad | ldap" << std::endl;
  cout << "\n";
  generic_client_usage();
}

int main(int argc, char **argv)
{
  std::string val;
  vector<const char*> args;
  argv_to_vec(argc, (const char **)argv, args);
  if (args.empty()) {
    cerr << argv[0] << ": -h or --help for usage" << std::endl;
    exit(1);
  }
  if (ceph_argparse_need_usage(args)) {
    usage();
    exit(0);
  }

  auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
			 CODE_ENVIRONMENT_UTILITY, 0);
  common_init_finish(g_ceph_context);

  char *v{nullptr};
  v = getenv("RGW_ACCESS_KEY_ID");
  if (v) {
    access_key = v;
  }

  v = getenv("RGW_SECRET_ACCESS_KEY");
  if (v) {
    secret_key = v;
  }

  for (auto arg_iter = args.begin(); arg_iter != args.end();) {
    if (ceph_argparse_witharg(args, arg_iter, &val, "--access",
			      (char*) nullptr)) {
      access_key = val;
    } else if (ceph_argparse_witharg(args, arg_iter, &val, "--secret",
				     (char*) nullptr)) {
      secret_key = val;
    } else if (ceph_argparse_witharg(args, arg_iter, &val, "--ttype",
				     (char*) nullptr)) {
      for (const auto& ttype : {"ad", "ldap"}) {
	if (boost::iequals(val, ttype)) {
	  type = RGWToken::to_type(val);
	  break;
	}
      }
    } else if (ceph_argparse_flag(args, arg_iter, "--encode",
					    (char*) nullptr)) {
      do_encode = true;
    } else if (ceph_argparse_flag(args, arg_iter, "--decode",
					    (char*) nullptr)) {
      do_decode = true;
    } else if (ceph_argparse_flag(args, arg_iter, "--verbose",
					    (char*) nullptr)) {
      verbose = true;
    } else {
      ++arg_iter;
    }
  }

  if ((! do_encode) ||
      (type == RGWToken::TOKEN_NONE)) {
    return -EINVAL;
  }

  formatter = new JSONFormatter(true /* pretty */);

  RGWToken token(type, access_key, secret_key);
  if (do_encode) {
    token.encode_json(formatter);
    std::ostringstream os;
    formatter->flush(os);
    string token_str = os.str();
    if (verbose) {
      std::cout << "expanded token: " << token_str << std::endl;
      if (do_decode) {
	RGWToken token2(token_str);
	std::cout << "decoded expanded token: " << token2 << std::endl;
      }
    }
    std::cout << to_base64(token_str) << std::endl;
  }

  return 0;
}