загрузить crt в f5-ltm с помощью invoke-webrequest - PullRequest
0 голосов
/ 21 декабря 2018

Я пытаюсь загрузить ssl-сертификат в f5 REST API и не нашел никого, кто использовал powershell для этого.Я настроил invoke-webrequest вокруг этой страницы, которая использует curl f5-Dev-central

f5: BIG-IP 13.1.1 Build 0.0.4 Final

я получаю следующую ошибку

Invoke-webrequest : {"code":400,"message":"Chunk byte count 8802 in Content-Range header different from received buffer length 162","originalRequestBody":

это часть скрипта:

....
#read the size of the file with the correct encoding
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"

$file = [IO.File]::ReadAllBytes($pathtofile)
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$encodedfile = $enc.GetString($file)

#get range of bytes for entire file in start-end/total format
$range = "0-" + ($encodedfile.Length - 1) + "/" + $encodedfile.Length

#create parts for invoke-webrequest call 

#create header json
$headers = @{"Content-Range" = $range; Authorization = $basicAuthValue}

$uri = "https://$bigip/mgmt/shared/file-transfer/bulk/uploads/$nameofcert.crt"

$params = @{'command'="install";'name'="$nameofcert";'from-local-file'=$pathtofile}
$json = $params | ConvertTo-Json

#run the invoke
Invoke-webrequest -Method POST -uri $uri -Headers $Headers -Body $json -ContentType 'application/json'

1 Ответ

0 голосов
/ 02 января 2019

Таким образом, заголовок моего вопроса - загрузка crt - у меня все еще есть проблемы при создании профиля на f5, но я решил проблемы с загрузкой файла.

другая часть, с которой я боролся, былаполучение ключа и сертификата в правильном формате, который хотел f5, поэтому я перейду к тому, что я сделал для этого

я начал с файла .pfx: (обратите внимание, у меня установлен openssl на моем сервере Windows 2016)

openssl pkcs12 -in d:\pathtocert.pfx -out d:\pathtocrtfile.crt -clcerts
openssl pkcs12 -in d:\pathtocert.pfx -out d:\pathtokey.key -nocerts

, чтобы получить crt из PEM в формате DER, который вам необходим для x509 - это требуется для f5

openssl x509 -inform pem -in d:\pathtocrtfile.crt -outform der -out d:\pathtocrtfile.crt

ok, чтобы получить файлы на f5 (яя собираюсь избегать использования ftp или чего-то в этом роде - и просто использовать остальную часть icontrol)

$site = 'donsTest'   # hard coding the name for now / could be passed in as an arg
$year = get-date -UFormat "%Y"
$nameofcert = "$site-cer-$year"
$pair = 'f5user:password'
$pathtofile = 'd:\pathtocrtfile.crt'
$keypath = 'd:\pathtokey.key'
$nameofkey = "$site-key-$year"
$nameofprofile = "$site-ssl-$year"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$bigip = 'iporURLTOF5'

####################################
#  get crt file ready for upload
####################################

хорошо, давайте теперь поместим этот файл crt в тело нашего вызова REST

$file = [IO.File]::ReadAllBytes($pathtofile)
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$encodedfile = $enc.GetString($file)
$range = "0-" + ($encodedfile.Length - 1) + "/" + $encodedfile.Length  # you need to calculate the size of this file to use in the REST Header

$headers = @{"Content-Range" = $range; Authorization = $basicAuthValue}
$uri = "https://$bigip/mgmt/shared/file-transfer/bulk/uploads/$nameofcert"
$uploadresult = Invoke-webrequest -Method POST -uri $uri -Headers $Headers -Body $encodedfile -ContentType 'application/json' | ConvertFrom-Json
$temppath = $uploadresult.localFilePath

сейчасфайл загружается на f5 - нам нужно установить его на f5 как сертификат

### Add new certificate on the f5 from the file you just uploaded
class cert
{
    [string]$command
    [string]$name
    [string]$fromLocalFile
}

$cert = New-Object -TypeName cert
$cert.command = "install"
$cert.name = $nameofcert 
$cert.fromLocalFile = $temppath
$body = $cert | ConvertTo-Json
$headers = @{Authorization = $basicAuthValue}
$url = "https://$bigip/mgmt/tm/sys/crypto/cert"
Invoke-WebRequest $url -method Post -Body $body -Headers $Headers -ContentType "application/json"

ключ - тот же процесс, кроме URL для ключа установки

$url = "https://" + $bigip + "/mgmt/tm/sys/crypto/key"
Invoke-WebRequest $url -method Post -Body $body -Headers $Headers -ContentType "application/json" -Credential $credential | ConvertFrom-Json
...