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

Buildpack is converted to a supply buildpack #19

Merged
merged 10 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 3 additions & 12 deletions bin/compile
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
#!/bin/bash -e
# bin/compile <build-dir>
#!/bin/bash

echo "[cyberark-conjur-buildpack]: compiling"
# bin/compile is currently required, even though it's deprecated. This is likely a bug.

BUILD_DIR=$1
BIN_DIR=$(cd $(dirname $0); pwd)
BUILDPACK_DIR=$(dirname $BIN_DIR)

pushd ${BUILD_DIR}
mkdir -p .profile.d vendor
cp ${BUILDPACK_DIR}/vendor/conjur-env ./vendor/conjur-env
cp ${BUILDPACK_DIR}/lib/0001_retrieve-secrets.sh ./.profile.d/0001_retrieve-secrets.sh
popd
exit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding the newline here at the end?

21 changes: 0 additions & 21 deletions bin/decorate

This file was deleted.

4 changes: 0 additions & 4 deletions bin/detect

This file was deleted.

36 changes: 36 additions & 0 deletions bin/supply
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash -e
# bin/supply <build-dir> <cache-dir> <deps-dir> <index>

BUILD_DIR=$1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful to add a quick comment for each of the directories and their purpose/role, either here or in the README.

Copy link
Member

@jtuttle jtuttle Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments.

CACHE_DIR=$2
DEPS_DIR=$3
INDEX_DIR=$4

BIN_DIR=$(cd $(dirname $0); pwd)
BUILDPACK_DIR=$(dirname $BIN_DIR)

echo "[cyberark-conjur-buildpack]: supplying"

if [ ! -f $BUILD_DIR/secrets.yml ]; then
echo "No secrets.yml file found in $BUILD_DIR."
exit 1
fi

if [[ false = $(echo $VCAP_SERVICES | jq 'has("cyberark-conjur")') ]]; then
echo "No credentials for cyberark-conjur service found in VCAP_SERVICES."
exit 1
fi

pushd ${DEPS_DIR}/${INDEX_DIR}
# We add the lib/0001_retrieve-secrets.sh script to .profile.d so that it will
# be run automatically to retrieve secrets when the app starts.
mkdir -p .profile.d
cp ${BUILDPACK_DIR}/lib/0001_retrieve-secrets.sh ./.profile.d/0001_retrieve-secrets.sh
sed "s/__BUILDPACK_INDEX__/$INDEX_DIR/g" ./.profile.d/0001_retrieve-secrets.sh -i

# conjur-env reads a secrets.yml file and uses it to retrieve secrets from
# Conjur. We copy it to the dependency directory to make it accessible to the
# /.profile.d script. The /vendor subdirectory is just for convenience.
mkdir -p vendor
cp ${BUILDPACK_DIR}/vendor/conjur-env ./vendor/conjur-env
popd
2 changes: 1 addition & 1 deletion ci/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
image: postgres:9.3

conjur:
image: cyberark/conjur:0.3.0-stable
image: cyberark/conjur
command: server -a cucumber -f /empty.yml
environment:
CONJUR_ADMIN_PASSWORD: admin
Expand Down
6 changes: 4 additions & 2 deletions ci/features/buildpackless.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Feature: profile d scripts without the buildpack

@BUILD_DIR
Scenario: Populates environment with secrets from Conjur
Given the compile script is run against the app's root folder
Given the build directory has a secrets.yml file
And VCAP_SERVICES contains cyberark-conjur credentials
And the supply script is run against the app's root folder
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have two tests that validate what happens when

  • the build dir does not have secrets.yml, and/or
  • VCAP_SERVICES does not contain cyberark-conjur creds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These conditions are already tested in the supply.feature so I think we're okay.

And conjur-env is installed
And a root policy:
"""
Expand All @@ -26,7 +28,7 @@ Feature: profile d scripts without the buildpack
CONJUR_MULTI_LINE_SECRET: !var conjur_multi_line_secret_id
LITERAL_SECRET: some literal secret
"""
When the .profile.d scripts are sourced
When the retrieve secrets .profile.d script is sourced
And the 'env' command is run
Then the environment contains
"""
Expand Down
9 changes: 0 additions & 9 deletions ci/features/compile.feature

This file was deleted.

15 changes: 0 additions & 15 deletions ci/features/decorate.feature

This file was deleted.

6 changes: 0 additions & 6 deletions ci/features/detect.feature

This file was deleted.

4 changes: 3 additions & 1 deletion ci/features/profile_d.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Feature: profile d script

@BUILD_DIR
Scenario: Populates environment with secrets from Conjur
Given the 'compile' script is run
Given the build directory has a secrets.yml file
And VCAP_SERVICES contains cyberark-conjur credentials
And the supply script is run against the app's root folder
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here, should we test when the conditions are not right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm this feature appears to be identical to the buildpackless one. They use different step definitions on master, but they essentially boil down to the same thing. I think we should probably just remove the buildpackless one.

And conjur-env is installed
And a root policy:
"""
Expand Down
8 changes: 2 additions & 6 deletions ci/features/step_definitions/common_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
f.unlink
end

Given(/^the '([^"]*)' script is run$/) do |script|
step "the '#{ENV['BUILDPACK_BUILD_DIR']}/bin/#{script} #{@BUILD_DIR}' command is run"
end

Given(/^the compile script is run against the app's root folder$/) do
Given(/^the supply script is run against the app's root folder$/) do
step "the following command is run:", <<EOS
#{ENV['BUILDPACK_BUILD_DIR']}/bin/compile #{@BUILD_DIR}
#{ENV['BUILDPACK_BUILD_DIR']}/bin/supply #{@BUILD_DIR} #{@CACHE_DIR} #{@DEPS_DIR} #{@INDEX_DIR}
EOS
end

Expand Down
9 changes: 1 addition & 8 deletions ci/features/step_definitions/profile_d_steps.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
When(/^the retrieve secrets \.profile\.d script is sourced$/) do
@commands ||= []
@commands << <<EOL
. #{@BUILD_DIR}/.profile.d/0001_retrieve-secrets.sh #{@BUILD_DIR}
HOME=#{@BUILD_DIR} DEPS_DIR=#{@DEPS_DIR} . #{@DEPS_DIR}/#{@INDEX_DIR}/.profile.d/0001_retrieve-secrets.sh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here explaining the reason for passing in HOME / DEPS_DIR and the syntax being used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, added a comment.

EOL
end

Then(/^the environment contains$/) do |text|
expect(@output).to include(text)
end

When(/^the \.profile\.d scripts are sourced$/) do
@commands ||= []
@commands << <<EOL
. #{@BUILD_DIR}/.profile.d/0001_retrieve-secrets.sh #{@BUILD_DIR}
EOL
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
end

Then(/^the retrieve secrets \.profile\.d script is installed$/) do
expect(File.exist?("#{@BUILD_DIR}/.profile.d/0001_retrieve-secrets.sh")).to be_truthy
expect(File.exist?("#{@DEPS_DIR}/#{@INDEX_DIR}/.profile.d/0001_retrieve-secrets.sh")).to be_truthy
end
23 changes: 23 additions & 0 deletions ci/features/supply.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Feature: Supply script
Supply script installs conjur-env and .profile.d script

@BUILD_DIR
Scenario: Successfully installs conjur-env and .profile.d scripts
Given the build directory has a secrets.yml file
And VCAP_SERVICES contains cyberark-conjur credentials
When the supply script is run against the app's root folder
Then the result should have a 0 exit status
And conjur-env is installed
And the retrieve secrets .profile.d script is installed

@BUILD_DIR
Scenario: When the app does not have a secrets.yml file
When the supply script is run against the app's root folder
Then the result should have a 1 exit status

@BUILD_DIR
Scenario: When VCAP_SERVICES does not have Conjur credentials
Given the build directory has a secrets.yml file
And VCAP_SERVICES does not have a cyberark-conjur key
When the supply script is run against the app's root folder
Then the result should have a 1 exit status
9 changes: 8 additions & 1 deletion ci/features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@

Before('@BUILD_DIR') do
@BUILD_DIR = Dir.mktmpdir
@VENDOR_DIR = "#{@BUILD_DIR}/vendor"
@CACHE_DIR = Dir.mktmpdir
@DEPS_DIR = Dir.mktmpdir
@INDEX_DIR = "1"

Dir.mkdir(File.join(@DEPS_DIR, @INDEX_DIR), 0700)
@VENDOR_DIR = "#{@DEPS_DIR}/#{@INDEX_DIR}/vendor"
end

After('@BUILD_DIR') do
FileUtils.remove_entry_secure @BUILD_DIR
FileUtils.remove_entry_secure @CACHE_DIR
FileUtils.remove_entry_secure @DEPS_DIR
end

def reset_root_policy
Expand Down
9 changes: 5 additions & 4 deletions lib/0001_retrieve-secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ trap 'err_report $LINENO' ERR
#}
#'

# making sure that no tracing takes place, we're dealing with secrets here :)
# Prevent tracing to ensures secrets won't be leaked.
declare xtrace=""
case $- in
(*x*) xtrace="xtrace";;
esac
set +x

# inject secrets into environment
pushd $1
eval "$(./vendor/conjur-env)"
# $HOME points to the app directory, which should contains a secrets.yml file.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there documentation we can link to that will share how we know HOME and DEPS_DIR will be set when this script is run?

Copy link
Member

@jtuttle jtuttle Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately no. This is knowledge I gained from the examples linked to in Slack conversations (and the conversations themselves). This seems to be quite poorly documented.

pushd $HOME
# __BUILDPACK_INDEX__ is replaced by sed in the 'supply' script.
eval "$($DEPS_DIR/__BUILDPACK_INDEX__/vendor/conjur-env)"
popd

[ ! -z "$xtrace" ] && set -x
Expand Down