Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP Basic Authentication for httpok checks #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions superlance/httpok.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

doc = """\
httpok.py [-p processname] [-a] [-g] [-t timeout] [-c status_code] [-b inbody]
[-m mail_address] [-s sendmail] URL
[-A username:password] [-m mail_address] [-s sendmail] URL

Options:

Expand Down Expand Up @@ -82,6 +82,9 @@
-E -- not "eager": do not check URL / emit mail if no process we are
monitoring is in the RUNNING state.

-A -- Specify a username:password to use for basic authentication for the
GET request to the URL.

URL -- The URL to which to issue a GET request.

The -p option may be specified more than once, allowing for
Expand All @@ -94,10 +97,12 @@

"""

import base64
import os
import socket
import sys
import time

from superlance.compat import urlparse
from superlance.compat import xmlrpclib

Expand All @@ -114,11 +119,13 @@ def usage():
class HTTPOk:
connclass = None
def __init__(self, rpc, programs, any, url, timeout, status, inbody,
email, sendmail, coredir, gcore, eager, retry_time):
email, sendmail, coredir, gcore, eager, retry_time,
auth=None):
self.rpc = rpc
self.programs = programs
self.any = any
self.url = url
self.auth = auth
self.timeout = timeout
self.retry_time = retry_time
self.status = status
Expand Down Expand Up @@ -173,13 +180,20 @@ def runforever(self, test=False):

specs = self.listProcesses(ProcessStates.RUNNING)
if self.eager or len(specs) > 0:
headers = {'User-Agent': 'httpok'}
if self.auth is not None:
auth_base64 = (
base64.encodestring(self.auth)
.replace('\n', ''))
headers.update({
'Authorization': "Basic %s" % auth_base64
})

try:
for will_retry in range(
self.timeout // (self.retry_time or 1) - 1 ,
-1, -1):
try:
headers = {'User-Agent': 'httpok'}
conn.request('GET', path, headers=headers)
break
except socket.error as e:
Expand Down Expand Up @@ -299,13 +313,14 @@ def restart(self, spec, write):

def main(argv=sys.argv):
import getopt
short_args="hp:at:c:b:s:m:g:d:eE"
short_args="hp:at:c:A:b:s:m:g:d:eE"
long_args=[
"help",
"program=",
"any",
"timeout=",
"code=",
"authentication=",
"body=",
"sendmail_program=",
"email=",
Expand Down Expand Up @@ -336,6 +351,7 @@ def main(argv=sys.argv):
retry_time = 10
status = '200'
inbody = None
auth = None

for option, value in opts:

Expand Down Expand Up @@ -375,6 +391,9 @@ def main(argv=sys.argv):
if option in ('-E', '--not-eager'):
eager = False

if option in ('-A', '--authentication'):
auth = value

url = arguments[-1]

try:
Expand All @@ -388,7 +407,7 @@ def main(argv=sys.argv):
return

prog = HTTPOk(rpc, programs, any, url, timeout, status, inbody, email,
sendmail, coredir, gcore, eager, retry_time)
sendmail, coredir, gcore, eager, retry_time, auth)
prog.runforever()

if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions superlance/timeoutconn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from superlance.compat import httplib
import socket
import ssl

class TimeoutHTTPConnection(httplib.HTTPConnection):
"""A customised HTTPConnection allowing a per-connection
Expand Down Expand Up @@ -36,7 +37,6 @@ def connect(self):

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.timeout:
self.sock.settimeout(self.timeout)
sock.settimeout(self.timeout)
sock.connect((self.host, self.port))
ssl = socket.ssl(sock, self.key_file, self.cert_file)
self.sock = httplib.FakeSocket(sock, ssl)
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)