Class: TrainPlugins::Pwsh::Connection
- Inherits:
-
Train::Plugins::Transport::BaseConnection
- Object
- Train::Plugins::Transport::BaseConnection
- TrainPlugins::Pwsh::Connection
- Includes:
- Platform
- Defined in:
- lib/train-pwsh/connection.rb
Overview
You must inherit from BaseConnection.
Instance Method Summary collapse
- #file_via_connection(path) ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
-
#install_connect_graph_exchange ⇒ Object
Establishes connection for modules such as mggraph, exchangeonline.
- #install_connect_teams_pnp ⇒ Object
- #run_command_via_connection(script, session_type_hash) ⇒ Object
- #run_script_in_graph_exchange(script) ⇒ Object
- #run_script_in_teams_pnp(script) ⇒ Object
- #uri ⇒ Object
Methods included from 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 |
# File 'lib/train-pwsh/connection.rb', line 37 def initialize() # '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() 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}", []) @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() if exit_status_graph_exchange != 0 return exit_status_graph_exchange elsif exit_status_teams_pnp != 0 return exit_status_teams_pnp end end |
Instance Method Details
#file_via_connection(path) ⇒ Object
76 77 78 |
# File 'lib/train-pwsh/connection.rb', line 76 def file_via_connection(path) return Train::File::Local::Windows.new(self,path) end |
#install_connect_graph_exchange ⇒ Object
Establishes connection for modules such as mggraph, exchangeonline
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/train-pwsh/connection.rb', line 90 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_pnp ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/train-pwsh/connection.rb', line 124 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
80 81 82 83 84 85 86 87 88 |
# File 'lib/train-pwsh/connection.rb', line 80 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) else return CommandResult.new("","",0) end end |
#run_script_in_graph_exchange(script) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/train-pwsh/connection.rb', line 149 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
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/train-pwsh/connection.rb', line 162 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 |
#uri ⇒ Object
120 121 122 |
# File 'lib/train-pwsh/connection.rb', line 120 def uri return 'pwsh://' end |