forked from ntdt/splunk_filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splunk_filter.py
174 lines (125 loc) · 5.21 KB
/
splunk_filter.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python2.7
import ldap
import getopt
import sys, os
from cftools.cf import CF
from cftools.logger import logger
from ConfigParser import ConfigParser
SUCCESS = "--status=success"
FAILED = "--status=fail"
def userLogin( info ):
found_user = False
BIND_DN = 'cn=' + info['username'] + ',' + LDAP_USER_ROOT
BIND_PASS = info['password']
logger.info('userLogin - message="User logging in with domain and username" username="%s"' % ( BIND_DN ))
logger.info('userLogin - message="LDAP server %s"' % ( LDAP_SERVER ))
try:
ldap_connection = ldap.initialize(LDAP_SERVER)
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
ldap.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
ldap.set_option( ldap.OPT_X_TLS_DEMAND, True )
ldap.set_option(ldap.OPT_REFERRALS, 0)
ldap.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
# Debug Mode, uncomment this to see debug level information on the command line
#ldap.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
auth = ldap_connection.simple_bind_s(BIND_DN, BIND_PASS)
found_user = True
except Exception, e:
pass
if found_user:
print SUCCESS
logger.info('message="Login Success" type="login" outcome="success" user="%s" script_return="%s" domain_controller="%s"' % ( BIND_DN, SUCCESS, LDAP_SERVER ))
else:
print FAILED
logger.info('message="Login Failure" type="login" outcome="failure" user="%s" script_return="%s" domain_controller="%s"' % ( BIND_DN, FAILED, LDAP_SERVER ) )
def getUserInfo( infoIn ):
try:
cf = CF(CF_URL)
except Exception, e:
print(e)
logger.info('getUserInfo - message="unable to call PCF", curl="%s"' % ( CF_URL ))
else:
try:
cf.login(admin_user, admin_password)
except Exception, e:
print(e)
logger.info('getUserInfo - message="invalid PCF credentials", curl="%s" - admin_user="%s"' % ( CF_URL, admin_user ))
else:
try:
usr = cf.search_user(infoIn['username'])
outStr = SUCCESS + ' --userInfo=;' + usr['entity']['username'] + ';;user'
print outStr
except:
logger.info('getUserInfo - message="Error when searching user", curl="%s" - username="%s"' % ( CF_URL, infoIn['username'] ))
print FAILED
def getUsers( infoIn ):
print SUCCESS + ' --userInfo=;admin;;admin'
def getSearchFilter(infoIn):
try:
cf = CF(CF_URL)
cf.login(admin_user, admin_password)
usr = cf.search_user(infoIn['username'])
except:
logger.info('getSearchFilter - message="invalid PCF credentials", curl="%s" - admin_user="%s"' % ( CF_URL, admin_user ))
print FAILED
else:
try:
orgs = cf.search_orgs(usr['entity']['organizations_url'])
if len(orgs) > 1:
indexFilter = '--search_filter=index=pcf_syslog '
filter = ''
for org in orgs:
currentFilter = 'host=' + org['entity']['name'] + '.*'
if filter != '':
filter += ' OR '
filter+= currentFilter
print SUCCESS + ' ' + indexFilter + filter
else:
logger.info('getSearchFilter - message="no PCF organization found for", username="%s"' % ( infoIn['username'] ))
print FAILED
except:
logger.info('getSearchFilter - message="method being called for an invalid user", username="%s"' % ( infoIn['username'] ))
print FAILED
def readinputs():
'''
reads the inputs coming in and put them in a dict for processing.
'''
optlist, args = getopt.getopt(sys.stdin.readlines(), '', ['username=', 'password='])
returnDict = {}
for name, value in optlist:
returnDict[name[2:]] = value.strip()
return returnDict
def checkusername(username):
if not username:
return ''
if "\\" in username['username']:
return "domain_user"
else:
return ''
if __name__ == "__main__":
logging = logger()
logger = logging.get_logger('ldap_auth')
config = ConfigParser()
config.read('splunk_filter.conf')
CF_URL = config.get('cloudfoundry', 'CF_URL')
admin_user = config.get('cloudfoundry', 'admin_user')
admin_password = config.get('cloudfoundry', 'admin_password')
LDAP_SERVER = config.get('ldap', 'LDAP_SERVER')
LDAP_USER_ROOT = config.get('ldap', 'LDAP_USER_ROOT')
callname = sys.argv[1]
dictin = readinputs()
# find out if we are dealing with domain\username or just a username
usertype = checkusername(dictin)
if usertype == "domain_user":
DOMAIN_USER = True
logger.info('method "%s" called' % (callname))
if callname == "userLogin":
userLogin( dictin )
elif callname == "getUsers":
getUsers( dictin )
elif callname == "getUserInfo":
getUserInfo( dictin )
elif callname == "getSearchFilter":
getSearchFilter( dictin )
else:
print "ERROR unknown function call: " + callname