train-pgsql 1.0.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a5a7f95a8bbb5fa60e7582b24b6bd7ba271dd659ff66e524c13aa6f4790b86d
4
- data.tar.gz: b9350bb9dcad6c1ed426b0a58ff663d4f676e3058494a047f3ba754c0d439368
3
+ metadata.gz: bf9046c760c47e717c33a05f1044d3142036509cc0fa883ab7ddc6b9664dc8e0
4
+ data.tar.gz: 901fc8f8862e6eb8ae21d9c8340927653d875d476d55db2accbdff90688a4b5e
5
5
  SHA512:
6
- metadata.gz: 707700dcf6d3067f7082d2973a5c1f14235a78a4b026f3c042af70c1c9e63c20822b52fe0553731a05cb618f53e58833159e5f090d0a38068b17aaac5344552d
7
- data.tar.gz: 2e7a11fa24fff87cb75d7effa39646276b6ab7e589d97de2dd176b33af3d9d04c2c6ba9541c10f0fc6178bc222221ea8f6a01f8a76b6a6edf67b84d8ebaccea6
6
+ metadata.gz: b34564dc3c47a2be228d7ee67e833d490e533b683775aaac3050dbc17d490509f5935046ecef1b71bef4dcb2af1a5dd0120aa74fd897040fb554889a1135756f
7
+ data.tar.gz: bba454681a04de826b47e109927189a030b9718d17b2c4351b2e293ae73679e594dd746a3e3e22794f238fc757b064dba53c060d7c1d3b81157d65a42749273b
data/README.md CHANGED
@@ -5,26 +5,51 @@ This plugin allows applications that rely on Train to communicate via postgres S
5
5
 
6
6
  ## Installation
7
7
 
8
- You will have to build this gem yourself to install it as it is not yet on
9
- Rubygems.Org. For this there is a rake task which makes this a one-liner:
8
+ This is published to rubygems.org at https://rubygems.org/gems/train-pgsql.
10
9
 
11
- ```bash
12
- rake install:local
10
+ You can install the gem directly:
11
+ ```sh
12
+ gem install train-pgsql
13
+ ```
14
+
15
+ Or you can install it as an `inspec` plugin:
16
+ ```sh
17
+ inspec plugin install train-pgsql
13
18
  ```
14
19
 
15
20
  ## Transport parameters
16
21
 
17
- | Option | Explanation | Default |
18
- | ---------- | ------------------- | ---------- |
19
- | `host` | Hostname | (required) |
20
- | `user` | Username to connect | (required) |
21
- | `password` | Password to connect | (required) |
22
- | `database` | Database to connect | `postgres` |
23
- | `port` | Remote port | `5432` |
22
+ | Option | Explanation | Default | ENV VAR |
23
+ | ---------- | ------------------- | ---------- | ------------ |
24
+ | `host` | Hostname | (required) | `PGHOST` |
25
+ | `user` | Username to connect | (required) | `PGUSER` |
26
+ | `password` | Password to connect | (required) | `PGPASSWORD` |
27
+ | `database` | Database to connect | `postgres` | `PGDATABASE` |
28
+ | `port` | Remote port | `5432` | `PGPORT` |
29
+
30
+
31
+ ## Example use in inspec
32
+ Connect to the postgresql target as such:
33
+ ```bash
34
+ inspec shell -t pgsql://db.host.name --user 'username' --password 'supersecret' --insecure boolean
35
+ ```
36
+ or
37
+ ```bash
38
+ inspec exec -t pgsql://db.host.name --user 'username' --password 'supersecret' --insecure boolean
39
+ ```
40
+
41
+ Alternatively you can set all these as environment variables using the following variables and authenticate without the parameters in in the inspec command or the target
42
+ ```bash
43
+ export PGHOST='db.host.name'
44
+ export PGUSER='username'
45
+ export PGPASSWORD='supersecret'
46
+ export PGDATABASE='somedatabase'
47
+ inspec exec -t pgsql://
48
+ ```
24
49
 
25
- ## Example use
50
+ ## Example use from Ruby
26
51
 
27
- This will work for a Cisco IOS XE device with Telnet enabled:
52
+ This will work if you have postgresql running on `localhost`:
28
53
  ```ruby
29
54
  require "train"
30
55
  train = Train.create("pgsql", {
@@ -35,4 +60,45 @@ train = Train.create("pgsql", {
35
60
  conn = train.connection
36
61
  result = conn.run_command("show version\n")
37
62
  conn.close
38
- ```
63
+ ```
64
+ ## Local development
65
+ If you are building this on a Mac you may run into an issue trying to install this locally due to the `PG` gem not installing due to code signing issues.
66
+ You can build it and run it in a docker container.
67
+
68
+ ### Requirements
69
+ - Postgres running locally
70
+ - docker
71
+
72
+ ### Steps
73
+ 1) Build the `train-pgsql` gem
74
+ ```
75
+ rake build
76
+ ```
77
+ 2) Build the docker image.
78
+ Be sure to set the appropriate GEM_VERSION arg based upon the value of the [version.rb](./lib/train-pgsql/version.rb)
79
+ ```
80
+ docker build --build-arg GEM_VERSION=1.0.0 . -t train-pgsql-test
81
+ ```
82
+ 3) Run the test.rb file
83
+ ```
84
+ docker run -it --rm -v $(pwd)/test:/share -e PG_HOST="host.docker.internal" --entrypoint ruby train-pgsql-test test.rb
85
+ ```
86
+ _Note: This test assumes you are running postgres locally on a mac. If you are running postgres in some other location, update the `PG_HOST` environment variable appropriately. You may have to set the username/password in the `pgsql` train instantiation._
87
+
88
+ You should see output resembling the following:
89
+ ```
90
+ #<struct Train::Extras::CommandResult stdout="1", stderr="", exit_status=0>
91
+ ```
92
+
93
+ ## Deploy
94
+ To publish a new version to RubyGems:
95
+ 1) Ensure the [version.rb](./lib/train-pgsql/version.rb) has been bumped with the appropriate semver update.
96
+ 2) Run rake release
97
+ ```
98
+ rake release
99
+ ```
100
+ _Note: you may have to authenticate with rubygems to publish. The `deploy@stridehealth.com` group has a rubygems [account](https://rubygems.org/profiles/stridehealth) that is an owner of this gem._
101
+
102
+ # Acknowledgements
103
+ * [train-telnet](https://github.com/tecracer-chef/train-telnet)
104
+ * [train](https://github.com/inspec/train)
@@ -6,6 +6,7 @@ module TrainPlugins
6
6
  class Connection < Train::Plugins::Transport::BaseConnection
7
7
  def initialize(options)
8
8
  super(options)
9
+ enable_cache(:command)
9
10
  end
10
11
 
11
12
  def platform
@@ -24,7 +25,7 @@ module TrainPlugins
24
25
  end
25
26
 
26
27
  def uri
27
- "pgsql://#{options[:user]}@#{@options[:host]}:#{@options[:port]}/"
28
+ "pgsql://#{options[:user]}@#{@options[:host]}:#{@options[:port]}/#{@options[:database]}"
28
29
  end
29
30
 
30
31
  def run_command_via_connection(query)
@@ -33,16 +34,17 @@ module TrainPlugins
33
34
  exit_status = 0
34
35
 
35
36
  logger.debug format('[Pgsql] Sending command (%s) to %s:%d', query, @options[:host], @options[:port])
36
- connection.exec(query) do |result|
37
- result.each_row do |values|
38
- stdout += values.join(', ') + "\n"
37
+ begin
38
+ connection.exec(query) do |result|
39
+ result.each_row do |values|
40
+ stdout += values.join(', ') + "\n"
41
+ end
42
+ stdout = stdout.strip
39
43
  end
40
- stdout = stdout.strip
44
+ rescue PG::Error => e
45
+ stderr = e.message
41
46
  end
42
-
43
47
  CommandResult.new(stdout, stderr, exit_status)
44
- rescue PG::Error => e
45
- raise Train::TransportError, "Pgsql exec failed (#{e.message})"
46
48
  end
47
49
 
48
50
  def connection
@@ -1,22 +1,22 @@
1
- require "train-pgsql/connection"
1
+ require 'train-pgsql/connection'
2
2
 
3
3
  module TrainPlugins
4
4
  module Pgsql
5
5
  class Transport < Train.plugin(1)
6
- name "pgsql"
6
+ name 'pgsql'
7
7
 
8
- option :host, required: true
9
- option :user, required: true
10
- option :password, required: true
11
- option :database, default: "postgres"
12
- option :port, default: 5432
8
+ option :host, required: true, default: ENV['PGHOST']
9
+ option :user, required: true, default: ENV['PGUSER']
10
+ option :password, required: true, default: ENV['PGPASSWORD']
11
+ option :database, required: false, default: ENV['PGDATABASE'] || 'postgres'
12
+ option :port, required: false, default: ENV['PGPORT'] || 5432
13
13
 
14
14
  # Non documented options for development
15
- option :debug_pgsql, default: false
15
+ option :debug_pgsql, default: false
16
16
 
17
17
  def connection(_instance_opts = nil)
18
18
  @connection ||= TrainPlugins::Pgsql::Connection.new(@options)
19
19
  end
20
20
  end
21
21
  end
22
- end
22
+ end
@@ -1,5 +1,5 @@
1
1
  module TrainPlugins
2
2
  module Pgsql
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.2.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: train-pgsql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cris Barbero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-08 00:00:00.000000000 Z
11
+ date: 2020-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: train