Code Monkey home page Code Monkey logo

api-python's Introduction

Conjur Python2 API Client

A Python2 client for the Conjur API.

If you are looking for Python3 API client, please go to our new project page at https://github.com/cyberark/conjur-api-python3.

IMPORTANT: THIS API CLIENT IS NOT CURRENTLY ACTIVELY BEING SUPPORTED

Installation

This Conjur Python2 API requires Python 2.7.

Install from PyPI

pip install conjur

Note: If you have the pandoc package installed you may need to uninstall it for the above command to work. You can do so with pip uninstall pypandoc.

API Documentation

See the API documentation for details of all classes and methods.

Usage

Configuration

# The `config` member of the conjur.config module is a "global" Configuration
# used by new API instances by default.
from conjur.config import config

# Set the conjur appliance url.  This can also be provided
# by the CONJUR_APPLIANCE_URL environment variable.
config.appliance_url = 'https://conjur.example.com/api'

# Set the (PEM) certificate file. This is also configurable with the
# CONJUR_CERT_FILE environment variable.
config.cert_file = '/path/to/conjur-account.pem'

Creating and Using an API Instance

import conjur

# For God's sake, don't put passwords in your source code!
password = 'super-secret'
login = 'alice'

# Create an API instance that can perform actions as the user 'alice'
api = conjur.new_from_key(login, password)

# Use the API to fetch the value of a variable

secret = api.variable('my-secret').value()

print("The secret is '{}'".format(secret))

new_from_key accepts a Conjur username and an api_key or password (see the Conjur developer documentation for details about the distinction). This is useful if your script is authenticating as an particular Conjur identity rather than acting on behalf of a user who has provided their token.

When created using this method, the API will attempt to authenticate the first time a method requiring authorization is called. To force it to authenticate immediately, you can use the authenticate() method. An instance created with new_from_key will cache it's auth token indefinitely. Since Conjur auth tokens expire after 8 minutes, you can force an api instance to update its token by calling api.authenticate(cached=False) or by setting api.token = None.

Other Ways to Create an API Instance

If the host running your application has been assigned a Conjur identity new_from_netrc is the easiest way to create an API instance.

import conjur
from conjur.config import config

config.load('/etc/conjur.conf')
api = conjur.new_from_netrc('/etc/conjur.identity', config=config)

If you have an existing authentication token, for example when handling an HTTP request that contains an end user's token, use new_from_token to create your API instance.

import conjur
# ... some web magic

api = conjur.new_from_token(request.get_json()['user_token'])
salesforce_apikey = api.variable('sales/salesforce/api_key')

YAML file

Conjurized hosts will have this file placed at /etc/conjur.conf.

Running locally this will be your ~/.conjurrc file.

from conjur.config import config

config.load('/etc/conjur.conf')

Variables

You can create, fetch and update variables like so:

import os
import conjur

api = conjur.new_from_key(login='danny', api_key=os.getenv('CONJUR_API_KEY'))

loggly_token = api.create_variable(
    id='monitoring/loggly.com/api-token',
    value='dEet7Hib1oSh9g'
)

gis_database_password = api.variable('gis/postgres/password')
print(gis_database_password.value())

gis_database_password.add_value('lij6det8eJ7pIx')

If no id is given, a unique id will be generated. If a value is provided, it will be used to set the variable's initial value. When fetching a variable, you can pass a version keyword argument to value() to retrieve a specific version.

Users

Create a user alice with password super-secret.

alice = api.create_user('alice', password='super-secret')

Create a user bob without a password, and save the API key. When creating a Conjur user, the API is available in the response. However, retrieving the user in the future will not return the API key.

bob = api.create_user('bob')
bob_api_key = bob.api_key

print("Created user 'bob' with api key '{}'".format(bob_api_key))

Fetch a user named 'otto', and check whether or not it was found:

if api.user('otto').exists():
  print("Otto exists!")
else:
  print("Sorry, otto doesn't exist :-(")

Groups

Create a group named developers and add an existing user alice to it.

devs = api.create_group('developers')

Development

Clone this project and run:

pip install -r requirements.txt -r requirements_dev.txt

Run tests and linting with:

./jenkins.sh

PyPi

To publish to PyPi, you will need to convert this document to restructured text using pandoc:

pandoc --from=markdown --to=rst --output=README.rst README.md

Furthermore, you will likely need to have the pypandoc package installed for the markup to appear correctly on the PyPi site.

License

The Conjur Python API is licensed under Apache License 2.0 - see LICENSE for more details.

api-python's People

Contributors

brikelly avatar dividedmind avatar dustinmm80 avatar garymoon avatar hleb-rubanau avatar izgeri avatar jakequilty avatar jjmason avatar kgilpin avatar kstutsman-conjur avatar ryanprior avatar sgnn7 avatar skrislov avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

api-python's Issues

Python 3 Support

I know in the overview you state that it's targeted for python 2.7, but is there going to be a plan to support python 3 with this library? Python 2 end of life was announced for Jan 2020 and we are currently using this, but are starting to move all of our applications to python 3. If there isn't a plan to support python 3 we are going to have to figure something out, just curious if this was on the roadmap at all.

SSL verify

Currently, verify_ssl is set to false:

conjur.configure(
        appliance_url='https://my.conjur.master/api',
        account='netflix',
        verify_ssl=False
    )

Without this setting, we get the following error:

2014-04-28 11:20:57,291 [requests.packages.urllib3.connectionpool][INFO    ] Starting new HTTPS connection (1): my.conjur.master
2014-04-28 11:20:57,337 [salt.utils.event ][ERROR   ] Failed to execute runner: <bound method ReactWrap.runner of <salt.utils.event.ReactWrap object at 0x2395a90>>
Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/salt/utils/event.py", line 613, in run
    ret = l_fun(*f_call.get('args', ()), **f_call.get('kwargs', {}))
  File "/usr/lib/pymodules/python2.7/salt/utils/event.py", line 634, in runner
    return runner.low(fun, kwargs)
  File "/usr/lib/pymodules/python2.7/salt/runner.py", line 138, in low
    ret = l_fun(*f_call.get('args', ()), **f_call.get('kwargs', {}))
  File "/srv/runners/bastion_user.py", line 21, in init
    _add_conjur_user(user_id, pubkey)
  File "/srv/runners/bastion_user.py", line 73, in _add_conjur_user
    if not user.exists():
  File "/usr/local/lib/python2.7/dist-packages/Conjur-0.1.0-py2.7.egg/conjur/user.py", line 32, in exists
    resp = self.api.get(self.url(), check_errors=False)
  File "/usr/local/lib/python2.7/dist-packages/Conjur-0.1.0-py2.7.egg/conjur/api.py", line 125, in get
    return self.request('get', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Conjur-0.1.0-py2.7.egg/conjur/api.py", line 104, in request
    headers['Authorization'] = self.auth_header()
  File "/usr/local/lib/python2.7/dist-packages/Conjur-0.1.0-py2.7.egg/conjur/api.py", line 85, in auth_header
    token = self.authenticate()
  File "/usr/local/lib/python2.7/dist-packages/Conjur-0.1.0-py2.7.egg/conjur/api.py", line 74, in authenticate
    response = requests.post(url, self.api_key, verify=self.config.verify_ssl)
  File "/usr/local/lib/python2.7/dist-packages/requests-2.2.1-py2.7.egg/requests/api.py", line 88, in post
    return request('post', url, data=data, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests-2.2.1-py2.7.egg/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests-2.2.1-py2.7.egg/requests/sessions.py", line 383, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests-2.2.1-py2.7.egg/requests/sessions.py", line 486, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests-2.2.1-py2.7.egg/requests/adapters.py", line 385, in send
    raise SSLError(e)
SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Certificate validity should be verified.

~PK

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.