train-pgsql 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -10
- data/lib/train-pgsql/connection.rb +7 -5
- data/lib/train-pgsql/transport.rb +9 -9
- data/lib/train-pgsql/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cc43010edf32c388d28ccc471efab2426f34929ee85ab8740aa8ea1c966c8fe
|
4
|
+
data.tar.gz: baa255ee6e272fb9c5adefcffe319b4bf3f5379866b9944e432302a8638d9151
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1dfb0415d0c437914a18021c87456687887b38e2f51013610599b5fb2a28683a181291fe08664299ca2e900b03920eb5b5c48365919d274661783d30a77e0297
|
7
|
+
data.tar.gz: 4d8b58bfeea0f915a1a2aea0ffcc3d306972abcab389bc894f2cf6c4a8df7aed11eb616a8af5b746661a125c84499f85a28467948d3593a5f132bd515353856d
|
data/README.md
CHANGED
@@ -19,17 +19,45 @@ inspec plugin install train-pgsql
|
|
19
19
|
|
20
20
|
## Transport parameters
|
21
21
|
|
22
|
-
| Option | Explanation | Default |
|
23
|
-
| ---------- | ------------------- | ---------- |
|
24
|
-
| `host` | Hostname | (required) |
|
25
|
-
| `user` | Username to connect | (required) |
|
26
|
-
| `password` | Password to connect | (required) |
|
27
|
-
| `database` | Database to connect | `postgres` |
|
28
|
-
| `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
|
+
## Transport CommandResult
|
31
|
+
`connection.run_command('SQL HERE')` returns a CommandResult object. The `stdout` value is set to a hash with `fields` and `values`.
|
32
|
+
|
33
|
+
For Example:
|
34
|
+
```
|
35
|
+
#<struct Train::Extras::CommandResult stdout={"fields"=>["name", "owner", "acl"], "values"=>[["public", "postgres", "postgres=UC/postgres"], ["public", "postgres", "=UC/postgres"]]}, stderr="", exit_status=0>
|
36
|
+
```
|
37
|
+
|
29
38
|
|
30
|
-
## Example use
|
39
|
+
## Example use in inspec
|
40
|
+
Connect to the postgresql target as such:
|
41
|
+
```bash
|
42
|
+
inspec shell -t pgsql://db.host.name --user 'username' --password 'supersecret' --insecure boolean
|
43
|
+
```
|
44
|
+
or
|
45
|
+
```bash
|
46
|
+
inspec exec -t pgsql://db.host.name --user 'username' --password 'supersecret' --insecure boolean
|
47
|
+
```
|
31
48
|
|
32
|
-
|
49
|
+
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
|
50
|
+
```bash
|
51
|
+
export PGHOST='db.host.name'
|
52
|
+
export PGUSER='username'
|
53
|
+
export PGPASSWORD='supersecret'
|
54
|
+
export PGDATABASE='somedatabase'
|
55
|
+
inspec exec -t pgsql://
|
56
|
+
```
|
57
|
+
|
58
|
+
## Example use from Ruby
|
59
|
+
|
60
|
+
This will work if you have postgresql running on `localhost`:
|
33
61
|
```ruby
|
34
62
|
require "train"
|
35
63
|
train = Train.create("pgsql", {
|
@@ -41,7 +69,6 @@ conn = train.connection
|
|
41
69
|
result = conn.run_command("show version\n")
|
42
70
|
conn.close
|
43
71
|
```
|
44
|
-
|
45
72
|
## Local development
|
46
73
|
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.
|
47
74
|
You can build it and run it in a docker container.
|
@@ -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)
|
@@ -35,10 +36,11 @@ module TrainPlugins
|
|
35
36
|
logger.debug format('[Pgsql] Sending command (%s) to %s:%d', query, @options[:host], @options[:port])
|
36
37
|
begin
|
37
38
|
connection.exec(query) do |result|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
stdout = {
|
40
|
+
'fields' => result.fields,
|
41
|
+
'values' => result.values
|
42
|
+
}
|
43
|
+
# stdout = result.values
|
42
44
|
end
|
43
45
|
rescue PG::Error => e
|
44
46
|
stderr = e.message
|
@@ -1,22 +1,22 @@
|
|
1
|
-
require
|
1
|
+
require 'train-pgsql/connection'
|
2
2
|
|
3
3
|
module TrainPlugins
|
4
4
|
module Pgsql
|
5
5
|
class Transport < Train.plugin(1)
|
6
|
-
name
|
6
|
+
name 'pgsql'
|
7
7
|
|
8
|
-
option :host, required: true
|
9
|
-
option :user, required: true
|
10
|
-
option :password, required: true
|
11
|
-
option :database, default:
|
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,
|
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
|
data/lib/train-pgsql/version.rb
CHANGED
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:
|
4
|
+
version: 2.0.0
|
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-
|
11
|
+
date: 2020-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: train
|