Skip to content

Commit

Permalink
Merge pull request #6 from minsis/master
Browse files Browse the repository at this point in the history
Added http auth to client
  • Loading branch information
wbrefvem authored Jul 26, 2020
2 parents e0a17f2 + 10d275b commit efa6a3a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ ENV/
.pytest_cache/

Pipfile.lock
.vscode/
.vscode/

# IDE editors
.idea
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ pipenv install --dev

### Usage

For HTTP authentication you can pass a in username and password

```python
from jolokia import JolokiaClient


jc = JolokiaClient('http://my-jolokia-enabled-server.com/jolokia', 'my_login', 'my_password')
```

To get a single attribute of an MBean:

```python
Expand Down
4 changes: 2 additions & 2 deletions jolokia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
class JolokiaClient(object):
"""Main class for interacting with a single Jolokia agent"""

def __init__(self, base_url):
def __init__(self, base_url, username=None, password=None):

verify_url(base_url)

self.base_url = base_url
self.session = JolokiaSession()
self.session = JolokiaSession(username, password)

@require_params(['mbean', 'operation', 'arguments'], 'execute method has 3 required keyword argument: mbean, operation, and arguments')
def execute(self, **kwargs):
Expand Down
8 changes: 8 additions & 0 deletions jolokia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class JolokiaRequest(Request):
class JolokiaSession(Session):
"""Wraps requests.Session"""

def __init__(self, username=None, password=None):
"""Initialize the session with http authentication if provided"""

super().__init__()

if username and password:
self.auth = (username, password)

def simple_post(self, url, data=None):
"""Posts to url and returns de-serialized response"""
try:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_api_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from jolokia.api import JolokiaClient
from tests.base import JolokiaTestCase
from requests.exceptions import ConnectionError

import logging
import pytest


LOGGER = logging.getLogger(__name__)


class TestAPI(JolokiaTestCase):

def test_unresolvable_agent(self):

self.jc = JolokiaClient('http://trythelandcrab.com', 'myuser', 'mypasswd')

kwargs = {'mbean': 'java.lang:Memory', 'attribute': 'HeapMemoryUsage'}
pytest.raises(ConnectionError, self.jc.get_attribute, **kwargs)

def test_missing_agent(self):

self.jc = JolokiaClient('http://google.com', 'myuser', 'mypasswd')
kwargs = {'mbean': 'java.lang:Memory', 'attribute': 'HeapMemoryUsage'}
resp = self.jc.get_attribute(**kwargs)

assert resp.status_code != 200

0 comments on commit efa6a3a

Please sign in to comment.