Skip to content

Commit

Permalink
fix: handle ietf-restconf:errors
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb committed Jun 5, 2024
1 parent 3eca3d7 commit cb997e9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions sztp-agent/pkg/secureagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ package secureagent

import (
"bufio"
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"github.com/jaypipes/ghw"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -46,6 +48,7 @@ func extractfromLine(line, regex string, index int) string {

func (a *Agent) doTLSRequest(input string, url string, empty bool) (*BootstrapServerPostOutput, error) {
var postResponse BootstrapServerPostOutput
var errorResponse BootstrapServerErrorOutput

body := strings.NewReader(input)
r, err := http.NewRequest(http.MethodPost, url, body)
Expand Down Expand Up @@ -74,20 +77,36 @@ func (a *Agent) doTLSRequest(input string, url string, empty bool) (*BootstrapSe
log.Println("Error doing the request", err.Error())
return nil, err
}
defer res.Body.Close()

Check failure on line 80 in sztp-agent/pkg/secureagent/utils.go

View workflow job for this annotation

GitHub Actions / golangci

Error return value of `res.Body.Close` is not checked (errcheck)

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
log.Println("Error reading the request", err.Error())
return nil, err
}

decoder := json.NewDecoder(res.Body)
decoder := json.NewDecoder(bytes.NewReader(bodyBytes))
decoder.DisallowUnknownFields()
if !empty {
derr := decoder.Decode(&postResponse)
if derr != nil {
return nil, derr
errdecoder := json.NewDecoder(bytes.NewReader(bodyBytes))
errdecoder.DisallowUnknownFields()
eerr := errdecoder.Decode(&errorResponse)
if eerr != nil {
log.Println("Received unknown response", string(bodyBytes))
return nil, derr
}
return nil, errors.New("[ERROR] Expected conveyed-information" +
", received error type=" + errorResponse.IetfRestconfErrors.Error[0].ErrorType +
", tag=" + errorResponse.IetfRestconfErrors.Error[0].ErrorTag +
", message=" + errorResponse.IetfRestconfErrors.Error[0].ErrorMessage)
}
log.Println(postResponse)
}
if res.StatusCode != http.StatusOK {
return nil, errors.New("[ERROR] Status code received: " + strconv.Itoa(res.StatusCode) + " ...but status code expected: " + strconv.Itoa(http.StatusOK))
}
defer res.Body.Close()
return &postResponse, nil
}

Expand Down

0 comments on commit cb997e9

Please sign in to comment.