Code Monkey home page Code Monkey logo

conjur-api-ruby's Introduction

Conjur::API

Programmatic Ruby access to the Conjur API.

RDocs are available from the through the Ruby Gem details page

Using conjur-api-ruby with Conjur Open Source

Are you using this project with Conjur Open Source? Then we strongly recommend choosing the version of this project to use from the latest Conjur OSS suite release. Conjur maintainers perform additional testing on the suite release versions to ensure compatibility. When possible, upgrade your Conjur version to match the latest suite release; when using integrations, choose the latest suite release that matches your Conjur version. For any questions, please contact us on Discourse.

Installation

Add this line to your application's Gemfile:

gem 'conjur-api'

And then execute:

$ bundle

Or install it yourself as:

$ gem install conjur-api

Usage

Connecting to Conjur is a two-step process:

  • Configuration Instruct the API where to find the Conjur endpoint and how to secure the connection.
  • Authentication Provide the API with credentials that it can use to authenticate.

Configuration

The simplest way to configure the Conjur API is to use the configuration file stored on the machine. If you have configured the machine with conjur init, its default location is ~/.conjurrc.

The Conjur configuration process also checks /etc/conjur.conf for global settings. This is typically used in server environments.

For custom scenarios, the location of the file can be overridden using the CONJURRC environment variable.

You can load the Conjur configuration file using the following Ruby code:

require 'conjur/cli'
Conjur::Config.load
Conjur::Config.apply

Note this code requires the conjur-cli gem, which should also be in your gemset or bundle.

Authentication

Once Conjur is configured, the connection can be established like this:

conjur = Conjur::Authn.connect nil, noask: true

To authenticate, the API client must provide a login name and api_key. The Conjur::Authn.connect will attempt the following, in order:

  1. Look for login in environment variable CONJUR_AUTHN_LOGIN, and api_key in CONJUR_AUTHN_API_KEY
  2. Look for credentials on disk. The default credentials file is ~/.netrc. The location of the credentials file can be overridden using the configuration file netrc_path option.
  3. Prompt for credentials. This can be disabled using the option noask: true.

Connecting Without Files

It's possible to configure and authenticate the Conjur connection without using any files, and without requiring the conjur-cli gem.

To accomplish this, apply the configuration settings directly to the Conjur::Configuration object.

For example, specify the account and appliance_url (both of which are required) like this:

Conjur.configuration.account = 'my-account'
Conjur.configuration.appliance_url = 'https://conjur.mydomain.com/api'

You can also specify these values using environment variables, which is often a bit more convenient. Environment variables are mapped to configuration variables by prepending CONJUR_ to the all-caps name of the configuration variable. For example, appliance_url is CONJUR_APPLIANCE_URL, account is CONJUR_ACCOUNT.

In either case, you will also need to configure certificate trust. For example:

OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE.add_file "/etc/conjur-yourorg.pem"

Once Conjur is configured, you can create a new API client by providing a login and api_key:

Conjur::API.new_from_key login, api_key

Note that if you are connecting as a Host, the login should be prefixed with host/. For example: host/myhost.example.com, not just myhost.example.com.

Configuring RestClient

Conjur::Configuration allows optional configuration of the RestClient instance used by Conjur API to communicate with the Conjur server, via the options hash Conjur.configuration.rest_client_options.

The default value for the options hash is:

{
  ssl_cert_store: OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
}

For example, here's how you would configure the client to use a proxy and ssl_ca_file (instead of the default ssl_cert_store).

Conjur.configuration.rest_client_options = {
    ssl_ca_file: "ca_certificate.pem",
    proxy: "http://proxy.example.com/"
}

Contributing

We welcome contributions of all kinds to this repository. For instructions on how to get started and descriptions of our development workflows, please see our contributing guide.

License

This repository is licensed under Apache License 2.0 - see LICENSE for more details.

conjur-api-ruby's People

Contributors

apotterri avatar dividedmind avatar doodlesbykumbi avatar dustinmm80 avatar foxefj-cyber avatar garymoon avatar gl-johnson avatar hleb-rubanau avatar ismarc avatar izgeri avatar jakequilty avatar jjmason avatar john-odonnell avatar jtuttle avatar juniortaeza avatar jvanderhoof avatar kgilpin avatar khamugit avatar kstutsman-conjur avatar mfelgate avatar micahlee avatar neil-k-zero avatar orenbm avatar rpothier avatar ryanprior avatar sashacher avatar semyon-estrin avatar sgnn7 avatar szh avatar telday avatar

Stargazers

 avatar  avatar  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

conjur-api-ruby's Issues

Ruby client uses basic authentication for API key rotation events

Summary

Nightly builds have started to fail in our CI/CD pipelines and the error is reproducible locally.

Errors are:

  • cucumber features/host.feature:14 # Scenario: API key of a a host can be rotated.
  • SimpleCov failed with exit 1Coverage report generated for Cucumber Features, RSpec to /src/conjur-api/coverage/coverage.xml. 1415 / 1538 LOC (92.0%) covered.

Steps to Reproduce

  1. Check out repo locally
  2. Run ./test.sh

Expected Results

Passing tests

Actual Results (including error logs, if applicable)

Tests fail

Reproducible

  • Always
  • Sometimes
  • Non-Reproducible

Version/Tag number

Latest

Environment setup

Jenkins and local macOS env

Additional Information

The problem may have been caused by server behavior change in regards to rotation of API keys that can be found here.

Allow using multiple independent API connections

Currently a lot of methods are global or use a global state, which makes it difficult if not impossible to maintain multiple Conjur API connections (configured differently) in the same process. Just a look through the list if Conjur::API class methods reveals many offenders, the biggest being .url_for. It's likely removing that and fixing what breaks will be a good first step.

Ruby client library tests are run against the EE

The ruby client for the CyberArk Conjur API has tests today that are run only against the OSS.

As part of the configurable TLS change and because the client libraries are run and used by our customers we should additional tests that run against the EE to reduce the manual testing around these,

Split up jenkins.sh script

We recently moved our Jenkins configuration for master over to a pipeline by calling jenkins.sh from the Jenkinsfile. We would now like to split up the jenkins.sh script into more discrete steps to make the pipeline easier to work with.

Ruby API gem correctly handles Users/Hosts with `api_key_enabled`

Validate and fix any errors with the following workflows:

  • Loading Policy with users and hosts with API keys disabled shows policy load success/failure, but not API keys
  • Loading Policy updating users and hosts with previously enabled API keys shows policy load success/failure
  • Loading Policy updating users and hosts with previously disabled API keys shows policy load success/failure and generated API keys
  • Attempt to rotate disabled API results in a helpful error message
  • Attempt to authenticate as a user/host with a disabled API key results in a authentication failure

Related to: cyberark/conjur/issues/1359

Fix broken master build

The master branch of the Ruby API is currently failing on an error before the cucumber tests even get run. It's happening because a new version of Cucumber was just released and we do not have the gem version pinned. Pin it back to the latest working version.

Custom RestClient options

Is your feature request related to a problem? Please describe.

We need to configure custom connection parameters for accessing the Conjur API, like proxies, TLS certificates and other TLS-related options. However, we can’t apply these settings globally because we have other uses of RestClient with different parameters.

Describe the solution you would like

Given conjur-api has a configuration object, having an extra key for passing a Hash of RestClient options sounds like a natural solution.

Conjur.configuration.rest_client_options = {
  ssl_ca_file: "ca_certificate.pem",
  proxy: "http://proxy.example.com/"
}

I noticed conjur-api patches RestClient for applying custom parameters, meaning it potentially conflicts with other gems that use RestClient too. If conjur-api supported local RestClient options, that monkey patching would not be needed.

# Monkey patch RestClient::Request so it always uses
# :ssl_cert_store. (RestClient::Resource uses Request to send
# requests, so it sees :ssl_cert_store, too).
# @api private
class RestClient::Request

I also noticed Conjur::Configuration has options ssl_certificate and cert_file, but they do not seem to apply when using Conjur::API.new_from_key.

Describe alternatives you have considered

Isolating the calls to the Conjur in a separate Ruby environment so that the global variables do not interfere, but that sounds quite overkill.

optimizations branch

Is the optimizations branch ready to pull? Is there anything backwards-incompatible in it?

What's the best way to finish it off?

Tests failing in master on Conjur::API.new_from_key

Failing Jenkins Build

https://jenkins.conjur.net/job/cyberark--conjur-api-ruby/job/master/208/

Failing Tests

cucumber features/new_api.feature:5 # Scenario: From API key.
cucumber features/new_api.feature:13 # Scenario: From access token.
cucumber features/new_api.feature:24 # Scenario: From access token file.

Example Test Output

  Scenario: From access token file. # features/new_api.feature:24
    Given I run the code:           # features/step_definitions/api_steps.rb:1
      """
      token = Conjur::API.new_from_key("host/#{@host_id}", @host_api_key).token
      @temp_file = Tempfile.new("token.json")
      @temp_file.write(token.to_json)
      @temp_file.flush
      """
    Then I run the code:            # features/step_definitions/api_steps.rb:1
      """
      api = Conjur::API.new_from_token_file @temp_file.path
      expect(api.resource("cucumber:host:#{@host_id}")).to exist
      """
      expected #<Conjur::Host:0x000055650cbc2778 @id=#<Conjur::Id:0x000055650cbc2c50 @id="cucumber:host:app-515f935a...pocnhaOUc4QUZ5aDRqZ0xURzMwaGp3dmlycjQifQ==\""}, :username=>nil}, @v5_router=Conjur::API::Router::V5> to exist (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/api_steps.rb:2:in `eval'
      (eval):2:in `block in <top (required)>'
      ./features/step_definitions/api_steps.rb:2:in `eval'
      ./features/step_definitions/api_steps.rb:2:in `/^I(?: can)? run the code:$/'
      features/new_api.feature:32:in `Then I run the code:'

Period in Resource ID is not properly escaped

Problem Description

When we use fully_escape to make Resource IDs URL safe, we don't currently escape the period (.) character. This can lead to resource URLs resulting in a 404 response from the Conjur API because it parses the period as an extension (.json) instead of part of the URL path.

Example for user with ID my.user:

"GET /api/audit/roles/cucumber%3Auser%3Amy.user?limit=10 HTTP/1.1" 404

Expected Outcome:

Periods should be escaped with the sequence %2E. With this substitution the request above is correctly routed and handled:

"GET /api/audit/roles/cucumber%3Auser%3Amy%2Euser?limit=10 HTTP/1.1" 200

Migrate client configuration from `conjur-cli` gem to this gem

Is your feature request related to a problem? Please describe.

At current, the documentation recommends also installing the conjur-cli gem in order to configure this gem.

Describe the solution you would like

We'd like to move all of the configuration into this gem, and the CLI gem can depend on conjur-api instead of the other way around.

Describe alternatives you have considered

n/a

Additional context

Once an update is made here, the CLI will also need to be updated to depend on conjur-api and to remove duplicate code.

API instance breaks on token expiration

Currently the library doesn't re-request tokens on expiration. While it's not currently a problem (no long-running operations ATM), it's conceivable that it'll become one. Furthermore it makes interactive usage very inconvenient.

standardised CHANGELOG exists, and is validated via pipeline

If the repo has a changelog that doesn't meet the standard, do try to change earlier entries to match the standard.
If the repo doesn't have a changelog use this as a starter:

# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

Acceptance criteria

Provide a better "inspect"

Working with the conjur API in a ruby repl (e.g. pry) is frustrating
because it seems to use ruby's default Object#inspect. This results in
objects being printed like the following (with large strings in place
of the ellipses):

# <Conjur::Role:0x007fbbf19323d0
@block=nil, 
@options= 
{:headers=> 
{:authorization=> 
"..."}, 
:username=>"..."}, 
@url= 
"...">

It'd be really helpful if #inspect returned a more concise and
descriptive string like #<Conjur::Role 'stripe:layer:layer1'>.

Blank CONJUR_SSL_CERTIFICATE gives error

If I try to use the Ruby API with a CONJUR_SSL_CERTIFICATE environment variable set to a blank value, I get the error:
ERR OpenSSL::X509::CertificateError: header too long

Ideally the Ruby API will treat a blank value for this env var the same as if this env var were unset.

Implement a configuration setting to support v4 and v5 routes

Add a setting Configuration.major_version with corresponding environment variable CONJUR_MAJOR_VERSION. This setting will select v4 or v5 compatible routing.

Methods which are not available in the selected version will raise NoMethodError.

In configuration.rb:

add_option :major_version

Create a utility method which will delegate a method call to a v4 or v5 provider based on the configuration setting.

Use this utility method to implement the differences in behavior between v4 and v5.

Skeleton implementation on https://github.com/cyberark/conjur-api-ruby/tree/major_version

Latest tagged version doesn't match published RubyGem

Summary

The latest published gem is 5.3.2 from May 6. The version of the gem is consistent with the latest version bump from January 10, but it wasn't published on January 10 - and there are no commits in the project from May 6.

Expected Results

Gems are only published when the repo is tagged

Actual Results (including error logs, if applicable)

A gem was published without a tag

Additional Information

To address this, we should update the pipeline to publish on tag-triggered builds and ensure the CONTRIBUTING is clear that to release a new gem, you should add a tag to the project.

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.