Skip to content

Commit

Permalink
Fix: send full public-key file to htsget
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtamm committed May 23, 2024
1 parent d0399c9 commit e822a48
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions storage/htsget.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package storage

import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -128,36 +128,24 @@ func htsgetUrl(url, useProtocol string) (updatedUrl string, token string) {
return
}

func htsgetHeader() string {
func htsgetHeaderJson() string {
ensureKeyFiles()

file, err := os.Open(publicKeyFile)
b, err := os.ReadFile(publicKeyFile)
if err != nil {
fmt.Println("Could not read", publicKeyFile, "file, which should exist:", err)
fmt.Println("[ERROR] Could not read", publicKeyFile,
"file, which should exist:", err)
panic(1)
}

publicKey := ""
scanner := bufio.NewScanner(file)
if scanner.Scan() { // Skip one header line.
if scanner.Scan() {
publicKey = scanner.Text() // The key is on the second line.
}
}
file.Close()

// HTTP headers to be encoded as JSON:
headers := make(map[string]string)

if publicKey == "" {
fmt.Println("[WARN] Could not read public key (second line) from", publicKeyFile, "file.")
} else {
headers["client-public-key"] = publicKey
}
headers["client-public-key"] = base64.StdEncoding.EncodeToString(b)

headersJson, err := json.Marshal(&headers)
if err != nil {
fmt.Println("Failed to format JSON-header for passing client-public-key:", err)
fmt.Println("[ERROR] Failed to format JSON-header for passing",
"client-public-key:", err)
panic(1)
}

Expand All @@ -173,7 +161,7 @@ func htsgetArgs(url, useProtocol string, decrypt bool) []string {
}

if decrypt {
cmdArgs = append(cmdArgs, "--headers", htsgetHeader())
cmdArgs = append(cmdArgs, "--headers", htsgetHeaderJson())
}

cmdArgs = append(cmdArgs, httpsUrl)
Expand Down Expand Up @@ -261,11 +249,6 @@ func cmdPipe(cmd1, cmd2 *exec.Cmd, destFilePath string) {
stderr2 := new(bytes.Buffer)
r, w := io.Pipe()

if err != nil {
fmt.Printf("[ERROR] failed to create OS pipe: %v", err)
return
}

cmd1.Stdout = w
cmd1.Stderr = stderr1

Expand Down

0 comments on commit e822a48

Please sign in to comment.