Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the Module with Certificates / Token #26

Closed
robesmithjr opened this issue Sep 13, 2018 · 9 comments
Closed

Using the Module with Certificates / Token #26

robesmithjr opened this issue Sep 13, 2018 · 9 comments

Comments

@robesmithjr
Copy link

robesmithjr commented Sep 13, 2018

Hi,

First off, thanks for putting this module together. I am trying to enable authentication and would love to see some examples of using an SSL certificate or token for authentication.

I would be happy to try and help out with documentation or other needed items in return.

Thanks again,

Rob

@robesmithjr robesmithjr changed the title Using the Module with Cetiicates Using the Module with Certificates / token Sep 13, 2018
@robesmithjr robesmithjr changed the title Using the Module with Certificates / token Using the Module with Certificates / Token Sep 13, 2018
@jpsider
Copy link
Owner

jpsider commented Sep 14, 2018

Awesome!

Yes I have examples! Sadly I’ve not published them yet and they all use self signed certs. Do you have a cart? Or need me to include that too?

I will gladly take any help!

@robesmithjr
Copy link
Author

robesmithjr commented Sep 14, 2018 via email

@jpsider
Copy link
Owner

jpsider commented Sep 16, 2018

That's a very interesting use case! I'm hoping we can get this working for you, and confident we can! I've updated the 'Readme' with a lot more information and created a blog post on creating a local Certificate hierarchy. https://invoke-automation.blog/2018/09/16/creating-a-local-ssl-certificate-hierarchy-with-windows-powershell/

Thanks for pushing me to actually update the documentation, it was well over due! Please let me know if it helps or if you think I should provide additional information.

@robesmithjr
Copy link
Author

Hello,

I was able to get the Self Signed Rest Endpoint communicating with the client. When using Self Signed Certs, it is important to use the invoke-sslignore function from the module, you also have to disable SSL checks from the client side as well. Here is the function that I used for this:

function Ignore-SSLCertificates #ignore certificate errors
{
$Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler = $Provider.CreateCompiler()
$Params = New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable = $false
$Params.GenerateInMemory = $true
$Params.IncludeDebugInformation = $false
$Params.ReferencedAssemblies.Add("System.DLL") > $null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy
{
public class TrustAll : System.Net.ICertificatePolicy
{
public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
{
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
## We create an instance of TrustAll and attach it to the ServicePointManager
$TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
}

Use cases may vary, but this is what I had to do to get self signed certs working across two separate nodes (one hosting the Rest Endpoint and one or more clients. I had tried using several other methods for ignoring certs, there may be an easier way, but this worked well.

In addition, it is important to note that if the client will be not be running as administrator, the CurrentUser\My certificate store should be used. I exported the Server and Client Certs with the Private Keys and included all certificates in the chain, in order to get the Self Signed CA added to the Trusted Root store.

@robesmithjr
Copy link
Author

robesmithjr commented Sep 18, 2018

Hi Justin,

I am going to be wrapping several different REST endpoints through the Webproxy that I am writing. Sometimes there are quite a few parameters that need to be passed. I modified your code that splits the GET parameters to store them within a pscustomobject, making the code reusable.

For processing more numerous parameter sets (using GET), take a look at this piece of code:

<#
.DESCRIPTION
This script will return the body passed to the RestEndpoint.
.EXAMPLE
Invoke-GetProcess.ps1 -RequestArgs "Name=PowerShell&MainWindowTitle=RestPS"
.NOTES
This will return a json object, through the REST Endpoint, or a pscustomobject that can be used for
additional calls.

#>

param(
$RequestArgs
)

$requestobj=[pscustomobject]@{}
if ($RequestArgs -like "&")
{

$ArgumentPairs = $RequestArgs.split("&")

For ($i=0; $i -le $ArgumentPairs.count; $i++)

{
    $Property, $value = $ArgumentPairs[$i].split("=")
    $requestobj|Add-Member -MemberType NoteProperty -Name $property -value $value
}

#additional code here that will leverage the pscustomobject#
}

@jpsider
Copy link
Owner

jpsider commented Sep 19, 2018

I think I would make a slight change to the loop, but I completely agree with your logic!

$ArgumentPairs = $RequestArgs.split('&')
foreach ($ArgumentPair in $ArgumentPairs) {
    $Property, $Value = $ArgumentPair.split('=')
    $RequestObj | Add-Member -MemberType NoteProperty -Name $Property -value $Value
}

@robesmithjr
Copy link
Author

Hi Justin,

My implementation is going well, I have the certificates working for authentication to the service. I have the Rest endpoint connecting to several other services and populating a database with the results. Thank you for all of your great work and suggestions!

My next step is to log the incoming requests for debugging.

@jpsider
Copy link
Owner

jpsider commented Sep 27, 2018

That’s awesome!! I do want to implement standard logging! You may beat me to it! So we can compare notes. I’m hoping to use my logging module, but that may take me a week or two. I’m excited it’s all working for you!

@robesmithjr
Copy link
Author

robesmithjr commented Sep 28, 2018 via email

@jpsider jpsider closed this as completed Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants