1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00

173 lines
4.1 KiB
Go
Raw Normal View History

2021-04-26 17:52:50 +06:00
package azure
2020-11-19 20:47:18 +06:00
import (
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
"errors"
2020-11-19 20:47:18 +06:00
"fmt"
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
"io"
2020-11-19 20:47:18 +06:00
"net/http"
"net/url"
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
"strconv"
2020-11-19 20:47:18 +06:00
"strings"
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
2022-12-15 20:57:12 +06:00
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
"github.com/imgproxy/imgproxy/v3/config"
"github.com/imgproxy/imgproxy/v3/ctxreader"
2022-12-15 20:57:12 +06:00
"github.com/imgproxy/imgproxy/v3/httprange"
2020-11-19 20:47:18 +06:00
)
2021-04-26 17:52:50 +06:00
type transport struct {
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
client *azblob.Client
2020-11-19 20:47:18 +06:00
}
2021-04-26 17:52:50 +06:00
func New() (http.RoundTripper, error) {
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
var (
client *azblob.Client
2022-12-15 20:57:12 +06:00
sharedKeyCredential *azblob.SharedKeyCredential
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
defaultAzureCredential *azidentity.DefaultAzureCredential
err error
)
2022-12-15 20:57:12 +06:00
if len(config.ABSName) == 0 {
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
return nil, errors.New("IMGPROXY_ABS_NAME must be set")
2020-11-19 20:47:18 +06:00
}
2021-04-26 17:52:50 +06:00
endpoint := config.ABSEndpoint
2020-11-19 20:47:18 +06:00
if len(endpoint) == 0 {
2021-04-26 17:52:50 +06:00
endpoint = fmt.Sprintf("https://%s.blob.core.windows.net", config.ABSName)
2020-11-19 20:47:18 +06:00
}
2022-12-15 20:57:12 +06:00
2020-11-19 20:47:18 +06:00
endpointURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
2022-12-15 20:57:12 +06:00
if len(config.ABSKey) > 0 {
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
sharedKeyCredential, err = azblob.NewSharedKeyCredential(config.ABSName, config.ABSKey)
if err != nil {
return nil, err
}
2020-11-19 20:47:18 +06:00
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
client, err = azblob.NewClientWithSharedKeyCredential(endpointURL.String(), sharedKeyCredential, nil)
} else {
defaultAzureCredential, err = azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, err
}
2020-11-19 20:47:18 +06:00
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
client, err = azblob.NewClient(endpointURL.String(), defaultAzureCredential, nil)
}
2020-11-19 20:47:18 +06:00
if err != nil {
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
return nil, err
}
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
return transport{client}, nil
}
func (t transport) RoundTrip(req *http.Request) (*http.Response, error) {
container := req.URL.Host
key := req.URL.Path
2022-12-15 20:57:12 +06:00
statusCode := http.StatusOK
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
header := make(http.Header)
opts := &blob.DownloadStreamOptions{}
if r := req.Header.Get("Range"); len(r) != 0 {
start, end, err := httprange.Parse(r)
if err != nil {
return httprange.InvalidHTTPRangeResponse(req), err
}
if end != 0 {
length := end - start + 1
if end <= 0 {
length = blockblob.CountToEnd
}
opts.Range = blob.HTTPRange{
Offset: start,
Count: length,
}
}
statusCode = http.StatusPartialContent
}
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
result, err := t.client.DownloadStream(req.Context(), container, strings.TrimPrefix(key, "/"), opts)
2020-11-19 20:47:18 +06:00
if err != nil {
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
if azError, ok := err.(*azcore.ResponseError); !ok || azError.StatusCode < 100 || azError.StatusCode == 301 {
return nil, err
} else {
body := strings.NewReader(azError.Error())
return &http.Response{
StatusCode: azError.StatusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
ContentLength: int64(body.Len()),
Body: io.NopCloser(body),
Close: false,
Request: req,
}, nil
}
2020-11-19 20:47:18 +06:00
}
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
if config.ETagEnabled && result.ETag != nil {
azETag := string(*result.ETag)
header.Set("ETag", azETag)
2021-09-29 16:23:54 +06:00
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
if etag := req.Header.Get("If-None-Match"); len(etag) > 0 && azETag == etag {
2021-09-29 16:23:54 +06:00
return &http.Response{
StatusCode: http.StatusNotModified,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
Header: header,
2021-09-29 16:23:54 +06:00
ContentLength: 0,
Body: nil,
Close: false,
Request: req,
}, nil
}
}
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
header.Set("Accept-Ranges", "bytes")
2022-12-15 20:57:12 +06:00
contentLength := int64(0)
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
if result.ContentLength != nil {
2022-12-15 20:57:12 +06:00
contentLength = *result.ContentLength
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
header.Set("Content-Length", strconv.FormatInt(*result.ContentLength, 10))
}
if result.ContentType != nil {
header.Set("Content-Type", *result.ContentType)
}
if result.ContentRange != nil {
header.Set("Content-Range", *result.ContentRange)
}
if result.CacheControl != nil {
header.Set("Cache-Control", *result.CacheControl)
}
return &http.Response{
StatusCode: statusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: header,
2022-12-15 20:57:12 +06:00
ContentLength: contentLength,
Implement Azure Identity Authentication for Azure Transport (#1052) * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * Replace OTel AWS ECS detector with fixed fork * Update semconv in OTel * refactor: implement support for azure managed identities and service principals Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * task: implement ranged request support Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: check result.ETag for nil Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: implement null guards for content-length and content-type Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: use blockblob.CountToEnd instead of -1 for length Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * chore: run go mod tidy Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> * refactor: discard err returns Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Signed-off-by: Alan Brault <alan.brault@hardwareandtools.com> Co-authored-by: DarthSim <darthsim@gmail.com>
2022-12-15 07:51:07 -05:00
Body: ctxreader.New(req.Context(), result.Body, true),
Close: true,
Request: req,
}, nil
2020-11-19 20:47:18 +06:00
}