Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
authentication-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include "common/logging.h"
18 #include "rpc/authentication.h"
19 #include "util/network-util.h"
20 #include "util/thread.h"
21 
22 DECLARE_bool(enable_ldap_auth);
23 DECLARE_string(ldap_uri);
24 DECLARE_string(keytab_file);
25 DECLARE_string(principal);
26 
27 // These are here so that we can grab them early in main() - the kerberos
28 // init can clobber KRB5_KTNAME in PrincipalSubstitution.
29 static const char *env_keytab = NULL;
30 static const char *env_princ = NULL;
31 
32 #include "common/names.h"
33 
34 namespace impala {
35 
36 TEST(Auth, PrincipalSubstitution) {
37  string hostname;
38  ASSERT_TRUE(GetHostname(&hostname).ok());
39  SaslAuthProvider sa(false); // false means it's external
40  ASSERT_TRUE(sa.InitKerberos("service_name/_HOST@some.realm", "/etc/hosts").ok());
41  ASSERT_TRUE(sa.Start().ok());
42  ASSERT_EQ(string::npos, sa.principal().find("_HOST"));
43  ASSERT_NE(string::npos, sa.principal().find(hostname));
44  ASSERT_EQ("service_name", sa.service_name());
45  ASSERT_EQ(hostname, sa.hostname());
46  ASSERT_EQ("some.realm", sa.realm());
47 }
48 
49 TEST(Auth, ValidAuthProviders) {
50  ASSERT_TRUE(AuthManager::GetInstance()->Init().ok());
51  ASSERT_TRUE(AuthManager::GetInstance()->GetExternalAuthProvider() != NULL);
52  ASSERT_TRUE(AuthManager::GetInstance()->GetInternalAuthProvider() != NULL);
53 }
54 
55 // Set up ldap flags and ensure we make the appropriate auth providers
56 TEST(Auth, LdapAuth) {
57  AuthProvider* ap = NULL;
58  SaslAuthProvider* sa = NULL;
59 
60  FLAGS_enable_ldap_auth = true;
61  FLAGS_ldap_uri = "ldaps://bogus.com";
62 
63  // Initialization based on above "command line" args
64  ASSERT_TRUE(AuthManager::GetInstance()->Init().ok());
65 
66  // External auth provider is sasl, ldap, but not kerberos
68  ASSERT_TRUE(ap->is_sasl());
69  sa = dynamic_cast<SaslAuthProvider*>(ap);
70  ASSERT_TRUE(sa->has_ldap());
71  ASSERT_EQ("", sa->principal());
72 
73  // Internal auth provider isn't sasl.
75  ASSERT_FALSE(ap->is_sasl());
76 }
77 
78 // Set up ldap and kerberos flags and ensure we make the appropriate auth providers
79 TEST(Auth, LdapKerbAuth) {
80  AuthProvider* ap = NULL;
81  SaslAuthProvider* sa = NULL;
82 
83  if ((env_keytab == NULL) || (env_princ == NULL)) {
84  return; // In a non-kerberized environment
85  }
86  FLAGS_keytab_file = env_keytab;
87  FLAGS_principal = env_princ;
88  FLAGS_enable_ldap_auth = true;
89  FLAGS_ldap_uri = "ldaps://bogus.com";
90 
91  // Initialization based on above "command line" args
92  ASSERT_TRUE(AuthManager::GetInstance()->Init().ok());
93 
94  // External auth provider is sasl, ldap, and kerberos
96  ASSERT_TRUE(ap->is_sasl());
97  sa = dynamic_cast<SaslAuthProvider*>(ap);
98  ASSERT_TRUE(sa->has_ldap());
99  ASSERT_EQ(FLAGS_principal, sa->principal());
100 
101  // Internal auth provider is sasl and kerberos
103  ASSERT_TRUE(ap->is_sasl());
104  sa = dynamic_cast<SaslAuthProvider*>(ap);
105  ASSERT_FALSE(sa->has_ldap());
106  ASSERT_EQ(FLAGS_principal, sa->principal());
107 }
108 
109 }
110 
111 int main(int argc, char** argv) {
114  ::testing::InitGoogleTest(&argc, argv);
115 
116  env_keytab = getenv("KRB5_KTNAME");
117  env_princ = getenv("MINIKDC_PRINC_IMPALA");
118 
119  return RUN_ALL_TESTS();
120 }
static const char * env_princ
AuthProvider * GetInternalAuthProvider()
DECLARE_bool(enable_ldap_auth)
const std::string & principal() const
Used for testing.
Definition: auth-provider.h:98
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
const std::string & hostname() const
Status InitKerberos(const std::string &principal, const std::string &keytab_path)
DECLARE_string(ldap_uri)
void InitGoogleLoggingSafe(const char *arg)
Definition: logging.cc:55
void InitThreading()
Initialises the threading subsystem. Must be called before a Thread is created.
Definition: thread.cc:261
static const char * env_keytab
AuthProvider * GetExternalAuthProvider()
Status GetHostname(string *hostname)
Definition: network-util.cc:40
static AuthManager * GetInstance()
const std::string & service_name() const
Definition: auth-provider.h:99
int main(int argc, char **argv)
const std::string & realm() const
bool ok() const
Definition: status.h:172
virtual bool is_sasl()=0
Returns true if this provider uses Sasl at the transport layer.