-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add subpackage to sign http.request with AWS V4 signature and parse response based on http.response and given model Other: - bump dependencies
- Loading branch information
Showing
7 changed files
with
251 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,8 @@ func WaitMinio(host string) bool { | |
} | ||
}() | ||
|
||
time.Sleep(5 * time.Second) | ||
|
||
return err == nil | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
sdkcrd "github.com/aws/aws-sdk-go/aws/credentials" | ||
sdksv4 "github.com/aws/aws-sdk-go/aws/signer/v4" | ||
) | ||
|
||
func CopyReader(r io.Reader) (io.ReadSeekCloser, error) { | ||
var tmp = bytes.NewBuffer(make([]byte, 0)) | ||
|
||
if _, err := io.Copy(tmp, r); err != nil { | ||
return nil, err | ||
} else { | ||
return &readerCloser{bytes.NewReader(tmp.Bytes())}, nil | ||
} | ||
} | ||
|
||
func NewReader(p []byte) io.ReadSeekCloser { | ||
return &readerCloser{bytes.NewReader(p)} | ||
} | ||
|
||
type readerCloser struct { | ||
io.ReadSeeker | ||
} | ||
|
||
func (r *readerCloser) Close() error { | ||
return nil | ||
} | ||
|
||
func Request(req *http.Request, cfg Config, service string) error { | ||
var ( | ||
err error | ||
sig = sdksv4.NewSigner(sdkcrd.NewStaticCredentials(cfg.GetAccessKey(), cfg.GetSecretKey(), "")) | ||
) | ||
|
||
if req.Body == nil { | ||
_, err = sig.Sign(req, nil, service, cfg.GetRegion(), time.Now()) | ||
} else if r, k := req.Body.(io.ReadSeekCloser); k { | ||
_, err = sig.Sign(req, r, service, cfg.GetRegion(), time.Now()) | ||
} else if r, err = CopyReader(req.Body); err != nil { | ||
return err | ||
} else { | ||
req.Body = r | ||
_, err = sig.Sign(req, r, service, cfg.GetRegion(), time.Now()) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
"mime" | ||
"net/http" | ||
) | ||
|
||
var ( | ||
ErrInvalidResponse = fmt.Errorf("invalid response") | ||
) | ||
|
||
type Config interface { | ||
GetRegion() string | ||
GetAccessKey() string | ||
GetSecretKey() string | ||
} | ||
|
||
type ErrorResponse struct { | ||
XMLName xml.Name `xml:"Error"` | ||
Code string `xml:"Code" json:"code"` | ||
Message string `xml:"Message" json:"message"` | ||
RequestID string `xml:"RequestId" json:"requestId"` | ||
} | ||
|
||
func (e ErrorResponse) Error() string { | ||
return fmt.Sprintf("request ID %s occure an aws response error %s: %s", e.RequestID, e.Code, e.Message) | ||
} | ||
|
||
type ErrorStatus struct { | ||
Status string `xml:"Code" json:"code"` | ||
Message string `xml:"Message" json:"message"` | ||
} | ||
|
||
func (e ErrorStatus) Error() string { | ||
return fmt.Sprintf("invalid response status code (%s): %s", e.Status, e.Message) | ||
} | ||
|
||
func Response(rsp *http.Response, model any) error { | ||
defer func() { | ||
if rsp != nil && rsp.Body != nil { | ||
_ = rsp.Body.Close() | ||
} | ||
}() | ||
|
||
var ( | ||
err error | ||
buf = bytes.NewBuffer(make([]byte, 0)) | ||
cnj = mime.TypeByExtension(".json") | ||
cnx = mime.TypeByExtension(".xml") | ||
) | ||
|
||
if rsp == nil { | ||
return ErrInvalidResponse | ||
} else if rsp.Body != nil { | ||
if _, e := io.Copy(buf, rsp.Body); e != nil { | ||
return e | ||
} | ||
} | ||
|
||
if tp := rsp.Header.Get("Content-Type"); tp == cnj { | ||
err = responseJson(buf, model) | ||
} else if tp != cnx { | ||
err = responseXml(buf, model) | ||
} else { | ||
return ErrInvalidResponse | ||
} | ||
|
||
if rsp.StatusCode < 200 || rsp.StatusCode >= 300 { | ||
if err != nil { | ||
return err | ||
} else { | ||
return &ErrorStatus{ | ||
Status: rsp.Status, | ||
Message: truncateBuf(buf), | ||
} | ||
} | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func truncateBuf(buf *bytes.Buffer) string { | ||
if buf.Len() > 255 { | ||
return buf.String()[:255] | ||
} else { | ||
return buf.String() | ||
} | ||
} | ||
|
||
func responseJson(buf *bytes.Buffer, model any) error { | ||
if e := json.Unmarshal(buf.Bytes(), model); e != nil { | ||
return responseJsonError(buf) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func responseJsonError(buf *bytes.Buffer) error { | ||
var err = ErrorResponse{} | ||
|
||
if e := json.Unmarshal(buf.Bytes(), &err); e != nil { | ||
return e | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
func responseXml(buf *bytes.Buffer, model any) error { | ||
if e := xml.Unmarshal(buf.Bytes(), model); e != nil { | ||
return responseXmlError(buf) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func responseXmlError(buf *bytes.Buffer) error { | ||
var err = ErrorResponse{} | ||
|
||
if e := xml.Unmarshal(buf.Bytes(), &err); e != nil { | ||
return e | ||
} else { | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters