2
我有一個調用TeamCity的REST API進行備份的PowerShell腳本。通過REST API身份驗證失敗的TeamCity備份
此腳本工作在v7.1上,但是當我升級到8.0.5時,腳本停止工作。
腳本:
$ErrorActionPreference = 'Stop'
function Execute-HTTPPostCommand()
{
param(
[string] $url
)
$webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::Default.GetBytes("")
$webrequest.ContentLength = $PostStr.Length
$webrequest.AuthenticationLevel = [System.Net.Security.AuthenticationLevel]::MutualAuthRequested
$webrequest.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0, $PostStr.length)
$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
function Execute-TeamCityBackup()
{
param(
[string] $server,
[string] $addTimestamp,
[string] $includeConfigs,
[string] $includeDatabase,
[string] $includeBuildLogs,
[string] $includePersonalChanges,
[string] $fileName
)
$TeamCityURL = [System.String]::Format("{0}/app/rest/server/backup?addTimestamp={1}&includeConfigs={2}&includeDatabase={3}&includeBuildLogs={4}&includePersonalChanges={5}&fileName={6}",
$server,
$addTimestamp,
$includeConfigs,
$includeDatabase,
$includeBuildLogs,
$includePersonalChanges,
$fileName);
Write-Host "URL: " $TeamCityURL
Execute-HTTPPostCommand $TeamCityURL
}
$server = "http://localhost"
$addTimestamp = $true
$includeConfigs = $true
$includeDatabase = $true
$includeBuildLogs = $true
$includePersonalChanges = $true
$fileName = "TeamCity_Backup_"
Execute-TeamCityBackup $server $addTimestamp $includeConfigs $includeDatabase $includeBuildLogs $includePersonalChanges $fileName
這失敗的消息 「遠程服務器返回錯誤:(500)內部服務器錯誤」。
從TeamCity的-的server.log:
[2013-12-17 17:53:12,419] ERROR - jetbrains.buildServer.SERVER - Error java.lang.IllegalArgumentException: Argument for @NotNull parameter 'key' of jetbrains/buildServer/controllers/interceptors/auth/impl/WaffleBasedNTLMHttpAuthenticationStrategy.getValue must not be null while processing request: POST '/runtimeError.jsp?addTimestamp=True&includeConfigs=True&includeDatabase=True&includePersonalChanges=True&includeBuildLogs=True&fileName=TeamCity_Backup_', from client 0:0:0:0:0:0:0:1:61236, no auth/user
java.lang.IllegalArgumentException: Argument for @NotNull parameter 'key' of jetbrains/buildServer/controllers/interceptors/auth/impl/WaffleBasedNTLMHttpAuthenticationStrategy.getValue must not be null
at jetbrains.buildServer.controllers.interceptors.auth.impl.WaffleBasedNTLMHttpAuthenticationStrategy.getValue(WaffleBasedNTLMHttpAuthenticationStrategy.java)
at jetbrains.buildServer.controllers.interceptors.auth.impl.WaffleBasedNTLMHttpAuthenticationStrategy.doProcessAuthenticationRequest(WaffleBasedNTLMHttpAuthenticationStrategy.java:57)
從TeamCity的-auth.log:
[2013-12-17 19:16:44,377] DEBUG [UA: null ; http-bio-80-exec-28] - Processing request with no authorization header: POST '/app/rest/server/backup?addTimestamp=True&includeConfigs=True&includeDatabase=True&includePersonalChanges=True&includeBuildLogs=True&fileName=TeamCity_Backup_', from client 0:0:0:0:0:0:0:1:55772, no auth/user
[2013-12-17 19:16:44,377] DEBUG [UA: null ; http-bio-80-exec-28] - No scheme was matched
[2013-12-17 19:16:44,377] DEBUG [UA: null ; http-bio-80-exec-28] - Processing unauthenticated request
[2013-12-17 19:16:44,377] DEBUG [UA: null ; http-bio-80-exec-28] - Responding with 401 HTTP status with message "Unauthorized", sending header in response: WWW-Authenticate: Basic realm="TeamCity", Basic realm="TeamCity", NTLM
[2013-12-17 19:16:44,377] DEBUG [UA: null ; http-bio-80-exec-26] - Processing request with authorization header: "NTLM": POST '/app/rest/server/backup?addTimestamp=True&includeConfigs=True&includeDatabase=True&includePersonalChanges=True&includeBuildLogs=True&fileName=TeamCity_Backup_', from client 0:0:0:0:0:0:0:1:55773, no auth/user, authorization data: "#########"
我已經測試腳本下的網頁瀏覽器(NTLM提示)運行證書和TC網站加載沒有問題。
任何想法我做錯了什麼?
我最終使用基本身份驗證作爲工作。我一直希望不要硬編碼用戶並通過ps文件。 [string] $ authorization =「user:pass」 \t $ binaryAuthorization = [System.Text.Encoding] :: UTF8.GetBytes($ authorization) $ authorization = [System.Convert] :: ToBase64String($ binaryAuthorization) \t $授權=「基本」+ $授權 $ webRequest.Headers.Add(「授權」,$授權) – Aaron0
有兩種方式存儲明文憑證。作爲一個解決方案,我構建了一個獲得Windows Credential Manager權限的模塊。 [獲取-StoredCredentials](http://www.automatedops.com/blog/2013/06/07/get-storedcredentials-module/) – logicaldiagram