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

[PS] Allow CI to publish the module #7091

Merged
merged 2 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# set $ErrorActionPreference to 'Stop' globally
$ErrorActionPreference = 'Stop'

# store the API client's configuration
# store the API client's configuration
$Script:Configuration = [System.Collections.HashTable]@{}

$Script:CmdletBindingParameters = @('Verbose','Debug','ErrorAction','WarningAction','InformationAction','ErrorVariable','WarningVariable','InformationVariable','OutVariable','OutBuffer','PipelineVariable')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ LONG DESCRIPTION

Frameworks supported:

* PowerShell 3.0+
* PowerShell {{{powershellVersion}}} or later
* .NET 4.0 or later
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ test_script:
$host.SetShouldExit($Result.FailedCount)
exit $Result.FailedCount
}
- ps: |
if ($env:APPVEYOR_REPO_TAG -eq $true -and $env:NuGetApiKey -ne $null)
{
choco install NuGet.CommandLine
Install-PackageProvider -Name NuGet -Force
Publish-Module -NuGetApiKey $env:NuGetApiKey -Path .\ -Confirm:$False -Verbose
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ a key. The 'Authorization' header is added to outbound HTTP requests.
Ref: https://openapi-generator.tech

.PARAMETER KeyId
KeyId for HTTP signing
KeyId for HTTP signing

.PARAMETER KeyFilePath
KeyFilePath for HTTP signing
Expand All @@ -397,7 +397,7 @@ KeyFilePath for HTTP signing
KeyPassPhrase, if the HTTP signing key is protected

.PARAMETER HttpSigningHeader
HttpSigningHeader list of HTTP headers used to calculate the signature. The two special signature headers '(request-target)' and '(created)'
HttpSigningHeader list of HTTP headers used to calculate the signature. The two special signature headers '(request-target)' and '(created)'
SHOULD be included.
The '(created)' header expresses when the signature was created.
The '(request-target)' header is a concatenation of the lowercased :method, an
Expand All @@ -408,11 +408,11 @@ If no headers are specified then '(created)' sets as default.
HashAlgrithm to calculate the hash, Supported values are "sha256" and "sha512"

.PARAMETER SigningAlgorithm
SigningAlgorithm specifies the signature algorithm, supported values are "RSASSA-PKCS1-v1_5" and "RSASSA-PSS"
SigningAlgorithm specifies the signature algorithm, supported values are "RSASSA-PKCS1-v1_5" and "RSASSA-PSS"
RSA key : Supported values "RSASSA-PKCS1-v1_5" and "RSASSA-PSS", for ECDSA key this parameter is not applicable

.PARAMETER SignatureValidityPeriod
SignatureValidityPeriod specifies the signature maximum validity time in seconds. It accepts integer value
SignatureValidityPeriod specifies the signature maximum validity time in seconds. It accepts integer value

.OUTPUTS

Expand Down Expand Up @@ -459,11 +459,11 @@ function Set-{{{apiNamePrefix}}}ConfigurationHttpSigning {
}
}

if ($keyType -eq "RSA" -and
if ($keyType -eq "RSA" -and
($SigningAlgorithm -ne "RSASSA-PKCS1-v1_5" -and $SigningAlgorithm -ne "RSASSA-PSS" )) {
throw "Provided Key and SigningAlgorithm : $SigningAlgorithm is not compatible."
}

if ($HttpSigningHeader -contains "(expires)" -and $SignatureValidityPeriod -le 0) {
throw "SignatureValidityPeriod must be greater than 0 seconds."
}
Expand All @@ -486,7 +486,7 @@ function Set-{{{apiNamePrefix}}}ConfigurationHttpSigning {
if ($null -ne $KeyPassPhrase) {
$httpSignatureConfiguration["KeyPassPhrase"] = $KeyPassPhrase
}

$Script:Configuration["HttpSigning"] = New-Object -TypeName PSCustomObject -Property $httpSignatureConfiguration
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
.SYNOPSIS
Gets the headers for HTTP signature.
.DESCRIPTION
Gets the headers for the http sigature.
Gets the headers for the http sigature.
.PARAMETER Method
HTTP method
HTTP method
.PARAMETER UriBuilder
UriBuilder for url and query parameter
.PARAMETER Body
Expand Down Expand Up @@ -44,49 +44,43 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
$TargetHost = $UriBuilder.Host
$httpSigningConfiguration = Get-{{{apiNamePrefix}}}ConfigurationHttpSigning
$Digest = $null

#get the body digest
$bodyHash = Get-{{{apiNamePrefix}}}StringHash -String $Body -HashName $httpSigningConfiguration.HashAlgorithm
if ($httpSigningConfiguration.HashAlgorithm -eq "SHA256") {
$Digest = [String]::Format("SHA-256={0}", [Convert]::ToBase64String($bodyHash))
}
elseif ($httpSigningConfiguration.HashAlgorithm -eq "SHA512") {
} elseif ($httpSigningConfiguration.HashAlgorithm -eq "SHA512") {
$Digest = [String]::Format("SHA-512={0}", [Convert]::ToBase64String($bodyHash))
}

$dateTime = Get-Date
#get the date in UTC
$currentDate = $dateTime.ToUniversalTime().ToString("r")

foreach ($headerItem in $httpSigningConfiguration.HttpSigningHeader) {
if ($headerItem -eq $HEADER_REQUEST_TARGET) {

if ($headerItem -eq $HEADER_REQUEST_TARGET) {
$requestTargetPath = [string]::Format("{0} {1}{2}", $Method.ToLower(), $UriBuilder.Path, $UriBuilder.Query)
$HttpSignatureHeader.Add($HEADER_REQUEST_TARGET, $requestTargetPath)
}
elseif ($headerItem -eq $HEADER_CREATED) {
} elseif ($headerItem -eq $HEADER_CREATED) {
$created = Get-{{{apiNamePrefix}}}UnixTime -Date $dateTime -TotalTime TotalSeconds
$HttpSignatureHeader.Add($HEADER_CREATED, $created)
}
elseif ($headerItem -eq $HEADER_EXPIRES) {
} elseif ($headerItem -eq $HEADER_EXPIRES) {
$expire = $dateTime.AddSeconds($httpSigningConfiguration.SignatureValidityPeriod)
$expireEpocTime = Get-{{{apiNamePrefix}}}UnixTime -Date $expire -TotalTime TotalSeconds
$HttpSignatureHeader.Add($HEADER_EXPIRES, $expireEpocTime)
}
elseif ($headerItem -eq $HEADER_HOST) {
} elseif ($headerItem -eq $HEADER_HOST) {
$HttpSignedRequestHeader[$HEADER_HOST] = $TargetHost
$HttpSignatureHeader.Add($HEADER_HOST.ToLower(), $TargetHost)
}
elseif ($headerItem -eq $HEADER_DATE) {
} elseif ($headerItem -eq $HEADER_DATE) {
$HttpSignedRequestHeader[$HEADER_DATE] = $currentDate
$HttpSignatureHeader.Add($HEADER_DATE.ToLower(), $currentDate)
}
elseif ($headerItem -eq $HEADER_DIGEST) {
} elseif ($headerItem -eq $HEADER_DIGEST) {
$HttpSignedRequestHeader[$HEADER_DIGEST] = $Digest
$HttpSignatureHeader.Add($HEADER_DIGEST.ToLower(), $Digest)
}elseif($RequestHeader.ContainsKey($headerItem)){
} elseif($RequestHeader.ContainsKey($headerItem)) {
$HttpSignatureHeader.Add($headerItem.ToLower(), $RequestHeader[$headerItem])
}else{
} else {
throw "Cannot sign HTTP request. Request does not contain the $headerItem header."
}
}
Expand All @@ -99,7 +93,7 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
}
#Concatinate headers value separated by new line
$headerValuesString = $headerValuesList -join "`n"

#Gets the hash of the headers value
$signatureHashString = Get-{{{apiNamePrefix}}}StringHash -String $headerValuesString -HashName $httpSigningConfiguration.HashAlgorithm

Expand All @@ -112,8 +106,7 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
-HashAlgorithmName $httpSigningConfiguration.HashAlgorithm `
-KeyPassPhrase $httpSigningConfiguration.KeyPassPhrase `
-SigningAlgorithm $httpSigningConfiguration.SigningAlgorithm
}
elseif ($KeyType -eq "EC") {
} elseif ($KeyType -eq "EC") {
$headerSignatureStr = Get-{{{apiNamePrefix}}}ECDSASignature -ECKeyFilePath $httpSigningConfiguration.KeyFilePath `
-DataToSign $signatureHashString `
-HashAlgorithmName $httpSigningConfiguration.HashAlgorithm `
Expand All @@ -134,10 +127,10 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
if ($HttpSignatureHeader.ContainsKey($HEADER_EXPIRES)) {
$authorizationHeaderValue += [string]::Format(",expires={0}", $HttpSignatureHeader[$HEADER_EXPIRES])
}
$authorizationHeaderValue += [string]::Format(",headers=""{0}"",signature=""{1}""",

$authorizationHeaderValue += [string]::Format(",headers=""{0}"",signature=""{1}""",
$headersKeysString , $headerSignatureStr)

$HttpSignedRequestHeader[$HEADER_AUTHORIZATION] = $authorizationHeaderValue
return $HttpSignedRequestHeader
}
Expand All @@ -147,7 +140,7 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
Gets the RSA signature

.DESCRIPTION
Gets the RSA signature for the http signing
Gets the RSA signature for the http signing
.PARAMETER PrivateKeyFilePath
Specify the API key file path
.PARAMETER DataToSign
Expand All @@ -168,11 +161,10 @@ function Get-{{{apiNamePrefix}}}RSASignature {
[securestring]$KeyPassPhrase
)
try {

if ($hashAlgorithmName -eq "sha256") {
$hashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA256
}
elseif ($hashAlgorithmName -eq "sha512") {
} elseif ($hashAlgorithmName -eq "sha512") {
$hashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA512
}

Expand All @@ -188,31 +180,26 @@ function Get-{{{apiNamePrefix}}}RSASignature {

if ($SigningAlgorithm -eq "RSASSA-PSS") {
$signedBytes = $rsa.SignHash($DataToSign, $hashAlgo, [System.Security.Cryptography.RSASignaturePadding]::Pss)
}
else {
} else {
$signedBytes = $rsa.SignHash($DataToSign, $hashAlgo, [System.Security.Cryptography.RSASignaturePadding]::Pkcs1)
}
}
else {
} else {
$rsa_provider_path = Join-Path -Path $PSScriptRoot -ChildPath "{{{apiNamePrefix}}}RSAEncryptionProvider.cs"
$rsa_provider_sourceCode = Get-Content -Path $rsa_provider_path -Raw
Add-Type -TypeDefinition $rsa_provider_sourceCode
Add-Type -TypeDefinition $rsa_provider_sourceCode

[System.Security.Cryptography.RSA]$rsa = [RSAEncryption.RSAEncryptionProvider]::GetRSAProviderFromPemFile($PrivateKeyFilePath, $KeyPassPhrase)

if ($SigningAlgorithm -eq "RSASSA-PSS") {
throw "$SigningAlgorithm is not supported on $($PSVersionTable.PSVersion)"
}
else {
} else {
$signedBytes = $rsa.SignHash($DataToSign, $hashAlgo, [System.Security.Cryptography.RSASignaturePadding]::Pkcs1)
}

}

$signedString = [Convert]::ToBase64String($signedBytes)
return $signedString
}
catch {
} catch {
throw $_
}
}
Expand All @@ -222,7 +209,7 @@ function Get-{{{apiNamePrefix}}}RSASignature {
Gets the ECDSA signature

.DESCRIPTION
Gets the ECDSA signature for the http signing
Gets the ECDSA signature for the http signing
.PARAMETER PrivateKeyFilePath
Specify the API key file path
.PARAMETER DataToSign
Expand All @@ -249,7 +236,7 @@ function Get-{{{apiNamePrefix}}}ECDSASignature {
throw "key file path does not exist."
}

if($PSVersionTable.PSVersion.Major -lt 7){
if ($PSVersionTable.PSVersion.Major -lt 7) {
throw "ECDSA key is not supported on $($PSVersionTable.PSVersion), Use PSVersion 7.0 and above"
}

Expand All @@ -263,27 +250,23 @@ function Get-{{{apiNamePrefix}}}ECDSASignature {
#$ecdsa = [System.Security.Cryptography.ECDsaCng]::New($cngKey)
$ecdsa = [System.Security.Cryptography.ECDsaCng]::New()
[int]$bytCount =0
if(![string]::IsNullOrEmpty($KeyPassPhrase)){
if (![string]::IsNullOrEmpty($KeyPassPhrase)) {
$ecdsa.ImportEncryptedPkcs8PrivateKey($KeyPassPhrase,$keyBytes,[ref]$bytCount)
} else {
$ecdsa.ImportPkcs8PrivateKey($keyBytes,[ref]$bytCount)
}
else{
$ecdsa.ImportPkcs8PrivateKey($keyBytes,[ref]$bytCount)
}


if ($HashAlgorithmName -eq "sha512") {
$ecdsa.HashAlgorithm = [System.Security.Cryptography.CngAlgorithm]::Sha512
}
else {
} else {
$ecdsa.HashAlgorithm = [System.Security.Cryptography.CngAlgorithm]::Sha256
}

$signedBytes = $ecdsa.SignHash($DataToSign)
$signedString = [System.Convert]::ToBase64String($signedBytes)
return $signedString

}


<#
.Synopsis
Gets the hash of string.
Expand All @@ -295,7 +278,7 @@ function Get-{{{apiNamePrefix}}}ECDSASignature {
Specifies the hash name to calculate the hash, Accepted values are "SHA1", "SHA256" and "SHA512"
It is recommneded not to use "SHA1" to calculate the Hash
.Outputs
String
String
#>
Function Get-{{{apiNamePrefix}}}StringHash {
param(
Expand All @@ -305,9 +288,9 @@ Function Get-{{{apiNamePrefix}}}StringHash {
[Parameter(Mandatory = $true)]
[ValidateSet("SHA1", "SHA256", "SHA512")]
$HashName
)
)
$hashAlogrithm = [System.Security.Cryptography.HashAlgorithm]::Create($HashName)
$hashAlogrithm.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))
$hashAlogrithm.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))
}

<#
Expand Down Expand Up @@ -359,7 +342,6 @@ function Get-{{{apiNamePrefix}}}CryptographicScheme {
return $SigningAlgorithm
}


<#
.Synopsis
Gets the key type from the pem file.
Expand Down Expand Up @@ -390,20 +372,16 @@ function Get-{{{apiNamePrefix}}}KeyTypeFromFile {

if ($key[0] -match $rsaPrivateKeyHeader -and $key[$key.Length - 1] -match $rsaPrivateFooter) {
$KeyType = "RSA"

}
elseif ($key[0] -match $ecPrivateKeyHeader -and $key[$key.Length - 1] -match $ecPrivateKeyFooter) {
} elseif ($key[0] -match $ecPrivateKeyHeader -and $key[$key.Length - 1] -match $ecPrivateKeyFooter) {
$keyType = "EC"
}
elseif ($key[0] -match $ecPrivateKeyHeader -and $key[$key.Length - 1] -match $ecPrivateKeyFooter) {
} elseif ($key[0] -match $ecPrivateKeyHeader -and $key[$key.Length - 1] -match $ecPrivateKeyFooter) {
<#this type of key can hold many type different types of private key, but here due lack of pem header
Considering this as EC key
#>
Considering this as EC key
#>
#TODO :- update the key based on oid
$keyType = "EC"
}
else {
} else {
throw "Either the key is invalid or key is not supported"
}
return $keyType
}
return $keyType
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace RSAEncryption
return binkey;
}
catch (System.FormatException)
{
{
StringReader str = new StringReader(pvkstr);

//-------- read PEM encryption info. lines and extract salt -----
Expand Down Expand Up @@ -234,7 +234,7 @@ namespace RSAEncryption
{
// ---- Now hash consecutively for count times ------
if (j == 0)
result = data00; //initialize
result = data00; //initialize
else
{
Array.Copy(result, hashtarget, result.Length);
Expand Down Expand Up @@ -276,4 +276,4 @@ namespace RSAEncryption
return decryptedData;
}
}
}
}
7 changes: 7 additions & 0 deletions samples/client/petstore/powershell/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ test_script:
$host.SetShouldExit($Result.FailedCount)
exit $Result.FailedCount
}
- ps: |
if ($env:APPVEYOR_REPO_TAG -eq $true -and $env:NuGetApiKey -ne $null)
{
choco install NuGet.CommandLine
Install-PackageProvider -Name NuGet -Force
Publish-Module -NuGetApiKey $env:NuGetApiKey -Path .\ -Confirm:$False -Verbose
}
Loading