Class: TrainPlugins::Pwsh::Connection

Inherits:
Train::Plugins::Transport::BaseConnection
  • Object
show all
Includes:
Platform
Defined in:
lib/train-pwsh/connection.rb

Overview

You must inherit from BaseConnection.

Instance Method Summary collapse

Methods included from Platform

#platform

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/train-pwsh/connection.rb', line 37

def initialize(options)
  # 'options' here is a hash, Symbol-keyed,
  # of what Train.target_config decided to do with the URI that it was
  # passed by `inspec -t` (or however the application gathered target information)
  # Some plugins might use this moment to capture credentials from the URI,
  # and the configure an underlying SDK accordingly.
  # You might also take a moment to manipulate the options.
  # Have a look at the Local, SSH, and AWS transports for ideas about what
  # you can do with the options.

  # Regardless, let the BaseConnection have a chance to configure itself.
  super(options)
  puts('Please wait a few minutes to let the Powershell modules download and connection get established... ')
  #Instance variables that store the necessary authentication credentials
  #@pwsh_session_graph_exchange = ::Pwsh::Manager.instance('/opt/homebrew/bin/pwsh', ['-NoLogo'])
  #@pwsh_session_teams_pnp = ::Pwsh::Manager.instance('/opt/homebrew/bin/pwsh', [])
  @pwsh_path = @options.delete(:pwsh_path)
  #@pwsh_session_graph_exchange = @options.delete(:graph_exchange_session)
  #@pwsh_session_teams_pnp = @options.delete(:teams_pnp_session)
  @pwsh_session_graph_exchange = ::Pwsh::Manager.instance("#{@pwsh_path}", ['-NoLogo'])
  @pwsh_session_teams_pnp = ::Pwsh::Manager.instance("#{@pwsh_path}", [])
  @pwsh_session_azure = ::Pwsh::Manager.instance("#{@pwsh_path}", ['-NoProfile'])
  @client_id = @options.delete(:client_id)
  @tenant_id = @options.delete(:tenant_id)
  @client_secret = @options.delete(:client_secret)
  @certificate_path = @options.delete(:certificate_path)
  @certificate_password = @options.delete(:certificate_password)
  @organization = @options.delete(:organization)
  @sharepoint_admin_url = @options.delete(:sharepoint_admin_url)
  
  exit_status_graph_exchange = install_connect_graph_exchange()
  exit_status_teams_pnp = install_connect_teams_pnp()
  exit_status_azure = install_azure()
  if exit_status_graph_exchange != 0
    return exit_status_graph_exchange
  elsif exit_status_teams_pnp != 0
    return exit_status_teams_pnp
  elsif exit_status_azure != 0
    return exit_status_azure
  end
  
end

Instance Method Details

#file_via_connection(path) ⇒ Object



80
81
82
# File 'lib/train-pwsh/connection.rb', line 80

def file_via_connection(path)
  return Train::File::Local::Windows.new(self,path)
end

#install_azureObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/train-pwsh/connection.rb', line 155

def install_azure()
  pwsh_azure_install_connect = %{
    #Collect designated inputs required for Graph, Exchange, and PnP connections
    $client_id = '#{@client_id}'
    $tenantid = '#{@tenant_id}'
    $certificate_password = '#{@certificate_password}'
    $certificate_path = '#{@certificate_path}'
    $sharepoint_admin_url = '#{@sharepoint_admin_url}'

    #Connect to Teams module
    If ($null -eq (Get-Module -ListAvailable -Name "Az")) {Install-Module Az -Force -AllowClobber}
    If ($null -eq (Get-Module -Name "Az")) {Import-Module Az}
    Connect-AzAccount
  }
  pwsh_azure_install_connect_result = @pwsh_session_azure.execute(pwsh_azure_install_connect)
  return pwsh_azure_install_connect_result[:exitcode]
end

#install_connect_graph_exchangeObject

Establishes connection for modules such as mggraph, exchangeonline



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/train-pwsh/connection.rb', line 96

def install_connect_graph_exchange()
  pwsh_graph_exchange_install_connect = %{
    #Collect designated inputs required for Graph and Exchange connections
    $client_id = '#{@client_id}'
    $tenantid = '#{@tenant_id}'
    $clientSecret = '#{@client_secret}'
    $certificate_password = '#{@certificate_password}'
    $certificate_path = '#{@certificate_path}'
    $organization = '#{@organization}'

    #Connect to Graph module
    If($null -eq (get-module -listavailable -name "microsoft.graph")){install-module microsoft.graph -Force -AllowClobber}
    If($null -eq (get-module -name "microsoft.graph")){import-module microsoft.graph}
    $password = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
    $ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential($client_id,$password)
    Connect-MgGraph -TenantId $tenantid -ClientSecretCredential $ClientSecretCredential -NoWelcome

    #Connect to Exchange module
    If($null -eq (get-module -listavailable -name "ExchangeOnlineManagement")){install-module ExchangeOnlineManagement -Force -AllowClobber}
    If($null -eq (get-module -name "ExchangeOnlineManagement")){import-module ExchangeOnlineManagement}
    $password = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
    $ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential($client_id,$password)
    Connect-IPPSSession -AppID $client_id -CertificateFilePath $certificate_path -CertificatePassword (ConvertTo-SecureString -String $certificate_password -AsPlainText -Force) -Organization $organization -ShowBanner:$false
    Connect-ExchangeOnline -CertificateFilePath $certificate_path -CertificatePassword (ConvertTo-SecureString -String $certificate_password -AsPlainText -Force)  -AppID $client_id -Organization $organization -ShowBanner:$false
  }
  
  pwsh_graph_exchange_install_connect_result = @pwsh_session_graph_exchange.execute(pwsh_graph_exchange_install_connect)
  return pwsh_graph_exchange_install_connect_result[:exitcode]
end

#install_connect_teams_pnpObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/train-pwsh/connection.rb', line 130

def install_connect_teams_pnp()
  pwsh_teams_pnp_install_connect = %{
    #Collect designated inputs required for Graph, Exchange, and PnP connections
    $client_id = '#{@client_id}'
    $tenantid = '#{@tenant_id}'
    $certificate_password = '#{@certificate_password}'
    $certificate_path = '#{@certificate_path}'
    $sharepoint_admin_url = '#{@sharepoint_admin_url}'

    #Connect to Teams module
    If($null -eq (get-module -listavailable -name "MicrosoftTeams")){install-module MicrosoftTeams -Force -AllowClobber}
    If($null -eq (get-module -name "MicrosoftTeams")){import-module MicrosoftTeams}
    $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certificate_path,$certificate_password)
    Connect-MicrosoftTeams -Certificate $cert -ApplicationId $client_id -TenantId $tenantid > $null

    #Connect to PnP module
    If($null -eq (get-module -listavailable -name "PnP.PowerShell")){install-module PnP.PowerShell -Force -AllowClobber}
    If($null -eq (get-module -name "PnP.PowerShell")){import-module PnP.PowerShell}
    $password = (ConvertTo-SecureString -AsPlainText $certificate_password -Force)
    Connect-PnPOnline -Url $sharepoint_admin_url -ClientId $client_id -CertificatePath $certificate_path -CertificatePassword $password -Tenant $tenantid
  }
  pwsh_teams_pnp_install_connect_result = @pwsh_session_teams_pnp.execute(pwsh_teams_pnp_install_connect)
  return pwsh_teams_pnp_install_connect_result[:exitcode]
end

#run_command_via_connection(script, session_type_hash) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/train-pwsh/connection.rb', line 84

def run_command_via_connection(script, session_type_hash)
  if session_type_hash.key?(:graph_exchange_session)
    return run_script_in_graph_exchange(script)
  elsif session_type_hash.key?(:teams_pnp_session)
    return run_script_in_teams_pnp(script)
  elsif session_type_hash.key?(:azure_session)
    return run_script_in_azure(script)
  else
    return CommandResult.new("","",0)
  end
end

#run_script_in_azure(script) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/train-pwsh/connection.rb', line 199

def run_script_in_azure(script)
  result = @pwsh_session_azure.execute(script)
  if result[:stdout].nil?
    result[:stdout] = ""
  end
  if !result[:stdout].empty? && result[:stdout].match?(/is not recognized|session is not established/i)
    result[:stderr] = result[:stdout]
    result[:stdout] = ""
    result[:exitcode] = -1
  end
  return CommandResult.new(result[:stdout],result[:stderr],result[:exitcode])
end

#run_script_in_graph_exchange(script) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/train-pwsh/connection.rb', line 173

def run_script_in_graph_exchange(script)
  result = @pwsh_session_graph_exchange.execute(script)
  if result[:stdout].nil?
    result[:stdout] = ""
  end
  if !result[:stdout].empty? && result[:stdout].match?(/is not recognized|session is not established/i)
    result[:stderr] = result[:stdout]
    result[:stdout] = ""
    result[:exitcode] = -1
  end
  return CommandResult.new(result[:stdout],result[:stderr],result[:exitcode])
end

#run_script_in_teams_pnp(script) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/train-pwsh/connection.rb', line 186

def run_script_in_teams_pnp(script)
  result = @pwsh_session_teams_pnp.execute(script)
  if result[:stdout].nil?
    result[:stdout] = ""
  end
  if !result[:stdout].empty? && result[:stdout].match?(/is not recognized|session is not established/i)
    result[:stderr] = result[:stdout]
    result[:stdout] = ""
    result[:exitcode] = -1
  end
  return CommandResult.new(result[:stdout],result[:stderr],result[:exitcode])
end

#uriObject



126
127
128
# File 'lib/train-pwsh/connection.rb', line 126

def uri
  return 'pwsh://'
end