train-vsphere 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: baa259e959105f850bbf08c00722541d608fe392cb9a39d3d8743e189bbc85f5
4
+ data.tar.gz: 1f4bb79dcc6a9fd466359a10bde359e292349b4fc22fc898de20ab65051c5924
5
+ SHA512:
6
+ metadata.gz: 79d39f4183add1adff0dbb9842c65951f8dd27c564ebfb3bd95b5054e37be262b39136c75c8a07a4adc9e71633e86292a3291326c1570e5d91dd95ab46db181f
7
+ data.tar.gz: 2472d1e48065f7ceb8141ec7c71ca7c12033bb875f6a1a0c6a98903e90d4d72b93d3ca68cc134d04d9a05862ab8923be3df86e4496ea8da358c620cd0e3555aa
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+ gem 'vsphere-automation-sdk', '~> 0.1.0'
7
+
8
+ group :development do
9
+ gem 'vsphere-automation-sdk', '~> 0.1.0'
10
+ end
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # Train-vsphere
2
+
3
+ `train-vsphere` is a Train plugin and is used as a Train Transport to connect to vsphere environments.
4
+
5
+ ## To Install this as a User
6
+
7
+ You will need InSpec v3.9 or later.
8
+
9
+ Simply run:
10
+
11
+ ```bash
12
+ $ inspec plugin install train-vsphere
13
+ ```
14
+
15
+ ## Using train-vsphere from InSpec
16
+ Connect to the vsphere target as such:
17
+ ```bash
18
+ inspec shell -t vsphere://vcenter.host.name --user 'username@sso.domain' --password 'supersecret' --insecure boolean
19
+ ```
20
+ or
21
+ ```bash
22
+ inspec exec -t vsphere://vcenter.host.name --user 'username@sso.domain' --password 'supersecret' --insecure boolean
23
+ ```
24
+
25
+ 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
26
+ ```bash
27
+ export VC_HOSTNAME='vcenter.host.name'
28
+ export VC_USERNAME='username@sso.domain'
29
+ export VC_PASSWORD='notVMware1!'
30
+ inspec exec -t vsphere://
31
+ ```
32
+
33
+ When connected, you can retrieve your API token in your resources or profiles as such:
34
+
35
+ ```ruby
36
+ #This retrieves an authentication token
37
+ @authtoken = inspec.backend.authenticate
38
+
39
+ #This authentication token can now be used to access all other APIs
40
+ VSphereAutomation::Appliance::AccessConsolecliApi.new(@authtoken).get.value
41
+ ```
42
+
43
+ An example of a resource
44
+ ```ruby
45
+
46
+ class Vcsa < Inspec.resource(1)
47
+ name 'vcsa'
48
+ supports platform: 'vsphere'
49
+ desc 'Use the vsphere audit resource to get information from the vSphere API'
50
+
51
+ def initialize
52
+ authenticate
53
+ end
54
+ def ssh
55
+ begin
56
+ return VSphereAutomation::Appliance::AccessConsolecliApi.new(@auth_token).get.value
57
+
58
+ rescue VSphereAutomation::ApiError => e
59
+ puts "Exception when calling AccessConsolecliApi->get: #{e}"
60
+ end
61
+ end
62
+
63
+ def exists?
64
+ return true
65
+ end
66
+
67
+ def authenticate
68
+ @auth_token = inspec.backend.authenticate
69
+ end
70
+ end
71
+
72
+ ```
73
+
74
+ And the matching control
75
+
76
+ ```ruby
77
+ control 'vcenter-appliance-VCSA-001-1' do # A unique ID for this control
78
+ impact 0.7 # The criticality, if this control fails.
79
+ title 'SSH should be disabled' # A human-readable title
80
+ desc 'SSH should be disabled by default'
81
+ # tag 'security'
82
+ # tag check: 'VCSA-001-1'
83
+ # ref 'https://docs.vmware.com/en/VMware-vSphere/6.7/com.vmware.vsphere.vcsa.doc/GUID-D58532F7-E48C-4BF2-87F9-99BA89BF659A.html'
84
+
85
+ describe vcsa do
86
+ it { should exist }
87
+ its('ssh') {should cmp 'false'}
88
+ end
89
+ end
90
+
91
+
92
+
93
+
94
+
95
+ ## Notes
96
+
97
+ Due to some unknown bug, libcurl4-gnutls-dev may be required on linux. I haven't tested this on various distributions yet. MacOS should work out of the box, but YMMV.
98
+
99
+ ## Contributing
100
+
101
+ 1. Fork it
102
+ 1. Create your feature branch (git checkout -b my-new-feature)
103
+ 1. Commit your changes (git commit -sam 'Add some feature')
104
+ 1. Push to the branch (git push origin my-new-feature)
105
+ 1. Create new Pull Request
106
+
107
+ ## License
108
+
109
+ | **Author:** | Sjors Robroek
110
+
111
+ | **Copyright:** | Copyright (c) 2019
112
+
113
+ | **License:** | Apache License, Version 2.0
114
+
115
+ Licensed under the Apache License, Version 2.0 (the "License");
116
+ you may not use this file except in compliance with the License.
117
+ You may obtain a copy of the License at
118
+
119
+ http://www.apache.org/licenses/LICENSE-2.0
120
+
121
+ Unless required by applicable law or agreed to in writing, software
122
+ distributed under the License is distributed on an "AS IS" BASIS,
123
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124
+ See the License for the specific language governing permissions and
125
+ limitations under the License.
data/lib/test.rb ADDED
@@ -0,0 +1,4 @@
1
+ basename = File.basename(__FILE__, ".rb")
2
+ libdir = File.expand_path("../#{basename}",__FILE__)
3
+ puts basename
4
+ puts libdir
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ require 'vsphere-automation-sdk'
5
+ require 'vsphere-automation-cis'
6
+ require 'train'
7
+ require 'train/plugins'
8
+ require 'train-vsphere/platform'
9
+ require 'vsphere-automation-appliance'
10
+ require 'vsphere-automation-content'
11
+ require 'vsphere-automation-vapi'
12
+ require 'vsphere-automation-vcenter'
13
+
14
+ module TrainPlugins
15
+ module Vsphere
16
+ class Connection < Train::Plugins::Transport::BaseConnection
17
+ include TrainPlugins::Vsphere::Platform
18
+
19
+
20
+ def initialize(options)
21
+
22
+ options = validate_options(options)
23
+ super(options)
24
+ enable_cache :api_call
25
+
26
+ end
27
+
28
+ def authenticate
29
+
30
+ return api_client unless cache_enabled?(:api_call)
31
+
32
+ @cache[:api_call][api_client.to_s.to_sym] ||= api_client
33
+ end
34
+
35
+
36
+
37
+
38
+ def uri
39
+ "vsphere://#{options[:hostname]}"
40
+ end
41
+
42
+
43
+ def local?
44
+ false
45
+ end
46
+
47
+ private
48
+
49
+ def api_client
50
+
51
+
52
+
53
+ configuration = VSphereAutomation::Configuration.new.tap do |c|
54
+ c.host = options[:host]
55
+ c.username = options[:user]
56
+ c.password = options[:password]
57
+ c.scheme = 'https'
58
+ c.verify_ssl = !options[:insecure]
59
+ c.verify_ssl_host = !options[:insecure]
60
+ end
61
+ begin
62
+ api_client = VSphereAutomation::ApiClient.new(configuration)
63
+ api_client.default_headers['Authorization'] = configuration.basic_auth_token
64
+ session_api = VSphereAutomation::CIS::SessionApi.new(api_client)
65
+ session_id = session_api.create('').value
66
+ api_client.default_headers['vmware-api-session-id'] = session_id
67
+ return api_client
68
+ rescue VSphereAutomation::ApiError => e
69
+ fail Train::ClientError
70
+ #puts "Exception when calling AccessConsolecliApi->get: #{e}"
71
+ end
72
+ end
73
+
74
+ def validate_options(options)
75
+ if options[:user].nil?
76
+ fail Train::ClientError,
77
+ 'A user needs to be set'
78
+ end
79
+ if options[:password].nil?
80
+ fail Train::ClientError,
81
+ 'A password needs to be set'
82
+ end
83
+ if options[:host].nil?
84
+ fail Train::ClientError,
85
+ 'A host needs to be set'
86
+ end
87
+
88
+
89
+ return options
90
+ end
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TrainPlugins
4
+ module Vsphere
5
+ module Platform
6
+ def platform
7
+ Train::Platforms.name('vsphere').in_family('cloud')
8
+ force_platform!('vsphere', release: TrainPlugins::Vsphere::VERSION)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+
@@ -0,0 +1,27 @@
1
+ require 'train'
2
+ require 'train/plugins'
3
+ require 'train-vsphere/connection'
4
+
5
+ # Train Plugins v1 are usually declared under the TrainPlugins namespace.
6
+ # Each plugin has three components: Transport, Connection, and Platform.
7
+ # We'll only define the Transport here, but we'll refer to the others.
8
+
9
+ module TrainPlugins
10
+ module Vsphere
11
+ class Transport < Train.plugin(1)
12
+ name 'vsphere'
13
+
14
+ option :host, required: true, default: ENV['VC_HOSTNAME']
15
+ option :user, required: true, default: ENV['VC_USERNAME']
16
+ option :password, required: true, default: ENV['VC_PASSWORD']
17
+ option :insecure, required: false, default: false
18
+
19
+
20
+
21
+ # inspec -t vsphere://
22
+ def connection(_instance_opts = nil)
23
+ @connection ||= TrainPlugins::Vsphere::Connection.new(@options)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ # @author Sjors Robroek
2
+
3
+ module TrainPlugins
4
+ module Vsphere
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ #basename = File.basename(__FILE__, ".rb")
4
+ #libdir = File.expand_path("../#{basename}/",__FILE__)
5
+ #$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
6
+
7
+ require 'train-vsphere/version'
8
+ require 'train-vsphere/transport'
9
+ require 'train-vsphere/platform'
10
+ require 'train-vsphere/connection'
@@ -0,0 +1,26 @@
1
+
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'train-vsphere/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'train-vsphere'
9
+ spec.version = TrainPlugins::Vsphere::VERSION
10
+ spec.authors = ['Sjors Robroek']
11
+ spec.email = ['s.robroek@vxsan.com']
12
+ spec.summary = 'Train Transport for vSphere'
13
+ spec.description = 'Allows applications using Train to speak to vSphere'
14
+ spec.homepage = 'https://github.com/srobroek/train-vsphere'
15
+ spec.license = 'Apache-2.0'
16
+
17
+ spec.files = %w{
18
+ README.md train-vsphere.gemspec Gemfile
19
+ } + Dir.glob(
20
+ 'lib/**/*', File::FNM_DOTMATCH
21
+ ).reject { |f| File.directory?(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'train', '~> 1.4'
25
+ spec.add_dependency 'vsphere-automation-sdk', '~> 0.1.0'
26
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: train-vsphere
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sjors Robroek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: train
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: vsphere-automation-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ description: Allows applications using Train to speak to vSphere
42
+ email:
43
+ - s.robroek@vxsan.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - README.md
50
+ - lib/test.rb
51
+ - lib/train-vsphere.rb
52
+ - lib/train-vsphere/connection.rb
53
+ - lib/train-vsphere/platform.rb
54
+ - lib/train-vsphere/transport.rb
55
+ - lib/train-vsphere/version.rb
56
+ - train-vsphere.gemspec
57
+ homepage: https://github.com/srobroek/train-vsphere
58
+ licenses:
59
+ - Apache-2.0
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Train Transport for vSphere
80
+ test_files: []