Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / test_rgw_ldap.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) 2015 New Dream Network
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 <stdint.h>
16 #include <tuple>
17 #include <iostream>
18 #include <vector>
19 #include <map>
20 #include <random>
21
22 #include "rgw/rgw_ldap.h"
23 #include "rgw/rgw_token.h"
24
25 #include "gtest/gtest.h"
26 #include "common/ceph_argparse.h"
27 #include "common/debug.h"
28 #include "global/global_init.h"
29
30 #define dout_subsys ceph_subsys_rgw
31
32 namespace {
33
34   struct {
35     int argc;
36     char **argv;
37   } saved_args;
38
39   bool do_hexdump = false;
40
41   string access_key("ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAibGRhcCIsCiAgICAgICAgImlkIjogImFkbWluIiwKICAgICAgICAia2V5IjogImxpbnV4Ym94IgogICAgfQp9Cg=="); // {admin,linuxbox}
42   string other_key("ewogICAgIlJHV19UT0tFTiI6IHsKICAgICAgICAidmVyc2lvbiI6IDEsCiAgICAgICAgInR5cGUiOiAibGRhcCIsCiAgICAgICAgImlkIjogImFkbWluIiwKICAgICAgICAia2V5IjogImJhZHBhc3MiCiAgICB9Cn0K"); // {admin,badpass}
43
44   string ldap_uri = "ldaps://f23-kdc.rgw.com";
45   string ldap_binddn = "uid=admin,cn=users,cn=accounts,dc=rgw,dc=com";
46   string ldap_bindpw = "supersecret";
47   string ldap_searchdn = "cn=users,cn=accounts,dc=rgw,dc=com";
48   string ldap_searchfilter = "";
49   string ldap_dnattr = "uid";
50
51   rgw::LDAPHelper ldh(ldap_uri, ldap_binddn, ldap_bindpw, ldap_searchdn,
52                       ldap_searchfilter, ldap_dnattr);
53
54 } /* namespace */
55
56 TEST(RGW_LDAP, INIT) {
57   int ret = ldh.init();
58   ASSERT_EQ(ret, 0);
59 }
60
61 TEST(RGW_LDAP, BIND) {
62   int ret = ldh.bind();
63   ASSERT_EQ(ret, 0);
64 }
65
66 TEST(RGW_LDAP, AUTH) {
67   using std::get;
68   using namespace rgw;
69   int ret = 0;
70   {
71     RGWToken token{from_base64(access_key)};
72     ret = ldh.auth(token.id, token.key);
73     ASSERT_EQ(ret, 0);
74   }
75   {
76     RGWToken token{from_base64(other_key)};
77     ret = ldh.auth(token.id, token.key);
78     ASSERT_NE(ret, 0);
79   }
80 }
81
82 TEST(RGW_LDAP, SHUTDOWN) {
83   // nothing
84 }
85
86 int main(int argc, char *argv[])
87 {
88   string val;
89   vector<const char*> args;
90
91   argv_to_vec(argc, const_cast<const char**>(argv), args);
92   env_to_vec(args);
93
94   for (auto arg_iter = args.begin(); arg_iter != args.end();) {
95     if (ceph_argparse_witharg(args, arg_iter, &val, "--access",
96                               (char*) nullptr)) {
97       access_key = val;
98     } else if (ceph_argparse_flag(args, arg_iter, "--hexdump",
99                                             (char*) nullptr)) {
100       do_hexdump = true;
101     } else {
102       ++arg_iter;
103     }
104   }
105
106   /* dont accidentally run as anonymous */
107   if (access_key == "") {
108     std::cout << argv[0] << " no AWS credentials, exiting" << std::endl;
109     return EPERM;
110   }
111
112   saved_args.argc = argc;
113   saved_args.argv = argv;
114
115   ::testing::InitGoogleTest(&argc, argv);
116   return RUN_ALL_TESTS();
117 }