33
44# Import required libs for sharepointhealth
55from .plugin_check import curlCheck # Change this to your own class
6+ from .oauth2_token import GetOauth2Token as Azureoauth
67import argparse
78import sys
89
2829def parse_args (args ):
2930 """
3031 Information extracted from: https://mkaz.com/2014/07/26/python-argparse-cookbook/
32+ https://docs.python.org/3/library/argparse.html
3133 :return: parse.parse_args(args) object
3234 You can use obj.option, example:
3335 options = parse_args(args)
3436 options.user # to read username
3537 """
36- parser = argparse .ArgumentParser (formatter_class = argparse .RawTextHelpFormatter )
38+ parser = argparse .ArgumentParser (formatter_class = argparse .RawTextHelpFormatter ,
39+ description = 'nagios plugin to check some url using curl and some other code' )
3740
3841 parser .add_argument ('-u' , '--url' , dest = 'url' , nargs = '?' , default = None , const = None ,
3942 help = 'url to check \n ' )
4043 parser .add_argument ('-e' , '--extra_args' , dest = 'extra_args' , nargs = '?' , default = None , const = None ,
4144 help = 'extra args to add to curl, see curl manpage \n ' )
45+
46+ # Arguments to check using OAuth2
47+ parser .add_argument ('--client_id' , dest = 'client_id' , nargs = '?' , default = None , const = None ,
48+ help = 'oauth2 client_id example client id: 6731de76-14a6-49ae-97bc-6eba6914391e \n ' )
49+ parser .add_argument ('--scope' , dest = 'scope' , nargs = '?' , default = None , const = None ,
50+ help = 'oauth2 scope https://company.onmicrosoft.com/some-unique-number-for-scope/.default \n ' )
51+ parser .add_argument ('--client_secret' , dest = 'client_secret' , nargs = '?' , default = None , const = None ,
52+ help = 'oauth2 client_secret client password \n ' )
53+ parser .add_argument ('--grant_type' , dest = 'grant_type' , nargs = '?' , default = 'client_credentials' , const = None ,
54+ help = 'oauth2 grant_type \n ' )
55+ parser .add_argument ('--auth_url' , dest = 'auth_url' , nargs = '?' , default = None , const = None ,
56+ help = 'oauth2 auth_url example: https://login.microsoftonline.com/company.onmicrosoft.com/oauth2/v2.0/token \n ' )
57+ parser .add_argument ('--oauth2' , action = 'store_true' ,
58+ help = '''Flag to use or not token for oauth2 before creating the request, used to check published services that uses azure oauth2 \n
59+ See https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#refresh-the-access-token \n ''' )
4260
4361 if not args :
4462 raise SystemExit (parser .print_help ())
@@ -50,8 +68,36 @@ def cli_execution(options):
5068 """
5169 : param: options: arguments from parse.parse_args(args) (see parse_args function)
5270 """
71+ auth_args = ''
72+ # Creating access_token to authenticate with azure oauth2
73+ if options .oauth2 :
74+ if not options .client_id :
75+ sys .exit ('param client_id is required when using oauth2' )
76+ if not options .scope :
77+ sys .exit ('param scope is required when using oauth2' )
78+ if not options .client_secret :
79+ sys .exit ('param client_secret is required when using oauth2' )
80+ if not options .auth_url :
81+ sys .exit ('param auth_url is required when using oauth2' )
82+
83+ oauth_obj = Azureoauth (client_id = options .client_id ,
84+ scope = options .scope ,
85+ client_secret = options .client_secret ,
86+ grant_type = options .grant_type ,
87+ auth_url = options .auth_url )
88+ auth_tuple = oauth_obj .get_token ()
89+ auth_http_code = auth_tuple [1 ]
90+ if auth_http_code != 200 :
91+ sys .exit ('Error getting access_token, http_code != 200, http_code: {}' .format (auth_http_code ))
92+
93+ access_token = auth_tuple [0 ]['access_token' ]
94+ header_token = {"Authorization" : "Bearer {}" .format (access_token )}
95+ # https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#use-the-access-token
96+ auth_args = "--header 'Authorization: {}'" .format (header_token ['Authorization' ])
97+
5398 curlnagiosobj = curlCheck (URL = options .url ,
54- extra_args = options .extra_args )
99+ extra_args = options .extra_args ,
100+ auth_args = auth_args )
55101
56102 def collect_data ():
57103 # use new object class HealthMonitor
0 commit comments