[PS] Allow CI to publish the module (#7091)

* better code format in powershell code

* add code to publish ps module
This commit is contained in:
William Cheng
2020-08-02 16:57:40 +08:00
committed by GitHub
parent 647d253ac8
commit 43471bacbc
12 changed files with 130 additions and 160 deletions

View File

@@ -23,5 +23,5 @@ LONG DESCRIPTION
Frameworks supported:
* PowerShell 3.0+
* PowerShell {{{powershellVersion}}} or later
* .NET 4.0 or later

View File

@@ -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
}

View File

@@ -5,7 +5,7 @@
.DESCRIPTION
Gets the headers for the http sigature.
.PARAMETER Method
HTTP method
HTTP method
.PARAMETER UriBuilder
UriBuilder for url and query parameter
.PARAMETER Body
@@ -49,8 +49,7 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
$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))
}
@@ -63,30 +62,25 @@ function Get-{{{apiNamePrefix}}}HttpSignedHeader {
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."
}
}
@@ -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 `
@@ -171,8 +164,7 @@ function Get-{{{apiNamePrefix}}}RSASignature {
if ($hashAlgorithmName -eq "sha256") {
$hashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA256
}
elseif ($hashAlgorithmName -eq "sha512") {
} elseif ($hashAlgorithmName -eq "sha512") {
$hashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA512
}
@@ -188,12 +180,10 @@ 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
@@ -202,17 +192,14 @@ function Get-{{{apiNamePrefix}}}RSASignature {
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 $_
}
}
@@ -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"
}
@@ -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.
@@ -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(
@@ -359,7 +342,6 @@ function Get-{{{apiNamePrefix}}}CryptographicScheme {
return $SigningAlgorithm
}
<#
.Synopsis
Gets the key type from the pem file.
@@ -390,19 +372,15 @@ 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

View File

@@ -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
}

View File

@@ -11,7 +11,7 @@
.DESCRIPTION
Gets the headers for the http sigature.
.PARAMETER Method
HTTP method
HTTP method
.PARAMETER UriBuilder
UriBuilder for url and query parameter
.PARAMETER Body
@@ -55,8 +55,7 @@ function Get-PSHttpSignedHeader {
$bodyHash = Get-PSStringHash -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))
}
@@ -69,30 +68,25 @@ function Get-PSHttpSignedHeader {
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-PSUnixTime -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-PSUnixTime -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."
}
}
@@ -118,8 +112,7 @@ function Get-PSHttpSignedHeader {
-HashAlgorithmName $httpSigningConfiguration.HashAlgorithm `
-KeyPassPhrase $httpSigningConfiguration.KeyPassPhrase `
-SigningAlgorithm $httpSigningConfiguration.SigningAlgorithm
}
elseif ($KeyType -eq "EC") {
} elseif ($KeyType -eq "EC") {
$headerSignatureStr = Get-PSECDSASignature -ECKeyFilePath $httpSigningConfiguration.KeyFilePath `
-DataToSign $signatureHashString `
-HashAlgorithmName $httpSigningConfiguration.HashAlgorithm `
@@ -177,8 +170,7 @@ function Get-PSRSASignature {
if ($hashAlgorithmName -eq "sha256") {
$hashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA256
}
elseif ($hashAlgorithmName -eq "sha512") {
} elseif ($hashAlgorithmName -eq "sha512") {
$hashAlgo = [System.Security.Cryptography.HashAlgorithmName]::SHA512
}
@@ -194,12 +186,10 @@ function Get-PSRSASignature {
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 "PSRSAEncryptionProvider.cs"
$rsa_provider_sourceCode = Get-Content -Path $rsa_provider_path -Raw
Add-Type -TypeDefinition $rsa_provider_sourceCode
@@ -208,17 +198,14 @@ function Get-PSRSASignature {
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 $_
}
}
@@ -255,7 +242,7 @@ function Get-PSECDSASignature {
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"
}
@@ -269,27 +256,23 @@ function Get-PSECDSASignature {
#$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.
@@ -301,7 +284,7 @@ function Get-PSECDSASignature {
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-PSStringHash {
param(
@@ -365,7 +348,6 @@ function Get-PSCryptographicScheme {
return $SigningAlgorithm
}
<#
.Synopsis
Gets the key type from the pem file.
@@ -396,19 +378,15 @@ function Get-PSKeyTypeFromFile {
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

View File

@@ -15,5 +15,5 @@ LONG DESCRIPTION
Frameworks supported:
* PowerShell 3.0+
* PowerShell 5.0 or later
* .NET 4.0 or later