Skip to content

Commit d9cf939

Browse files
[azopenai] Plumb InsecureAllowCredentialWithHTTP through every client constructor (Azure#23233)
Extending client to also allow insecure auth on all the client constructors using InsecureAllowCredentialWithHTTP.
1 parent 9844aee commit d9cf939

File tree

3 files changed

+118
-7
lines changed

3 files changed

+118
-7
lines changed

sdk/ai/azopenai/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
### Bugs Fixed
1010

11-
- Ai sdk custom_client now respects `InsecureAllowCredentialWithHTTP` flag for allowing insecure connections. (PR#23188)
11+
- Client now respects the `InsecureAllowCredentialWithHTTP` flag for allowing non-HTTPS connections. Thank you @ukrocks007! (PR#23188)
1212

1313
### Other Changes
1414

sdk/ai/azopenai/client_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ package azopenai_test
88

99
import (
1010
"context"
11+
"fmt"
1112
"net/http"
13+
"net/http/httptest"
1214
"reflect"
1315
"testing"
1416

1517
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
1618
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
19+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1720
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1821
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
22+
"github.com/Azure/azure-sdk-for-go/sdk/internal/test/credential"
1923
"github.com/stretchr/testify/require"
2024
)
2125

@@ -57,3 +61,93 @@ func TestClient_EmptyOptionsChecking(t *testing.T) {
5761
require.Emptyf(t, fields, "%T is ignored in our function signatures because it's empty", v)
5862
}
5963
}
64+
65+
func TestClient_InsecureHTTPAllowed(t *testing.T) {
66+
const fakeID = "fake-id"
67+
68+
hf := func(resp http.ResponseWriter, req *http.Request) {
69+
// _just_ enough of a response to prove we made it through the pipeline.
70+
_, err := resp.Write([]byte(fmt.Sprintf("{ \"id\": \"%s\" }", fakeID)))
71+
require.NoError(t, err)
72+
}
73+
74+
urlCh := make(chan string)
75+
76+
go func() {
77+
// start an HTTP service
78+
server := httptest.NewServer(http.HandlerFunc(hf))
79+
urlCh <- server.URL
80+
t.Cleanup(server.Close)
81+
}()
82+
83+
url := <-urlCh
84+
t.Logf(url)
85+
86+
t.Run("DefaultsToHTTPSOnly", func(t *testing.T) {
87+
client, err := azopenai.NewClientForOpenAI(url, azcore.NewKeyCredential("fake-key"), nil)
88+
require.NoError(t, err)
89+
90+
resp, err := client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
91+
Messages: []azopenai.ChatRequestMessageClassification{},
92+
}, nil)
93+
require.Empty(t, resp)
94+
require.EqualError(t, err, "authenticated requests are not permitted for non TLS protected (https) endpoints")
95+
96+
client, err = azopenai.NewClientWithKeyCredential(url, azcore.NewKeyCredential("fake-key"), nil)
97+
require.NoError(t, err)
98+
99+
resp, err = client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
100+
Messages: []azopenai.ChatRequestMessageClassification{},
101+
}, nil)
102+
require.Empty(t, resp)
103+
require.EqualError(t, err, "authenticated requests are not permitted for non TLS protected (https) endpoints")
104+
105+
fakeCred := &credential.Fake{}
106+
107+
client, err = azopenai.NewClient(url, fakeCred, nil)
108+
require.NoError(t, err)
109+
110+
resp, err = client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
111+
Messages: []azopenai.ChatRequestMessageClassification{},
112+
}, nil)
113+
require.Empty(t, resp)
114+
require.EqualError(t, err, "authenticated requests are not permitted for non TLS protected (https) endpoints")
115+
})
116+
117+
t.Run("InsecureAllowCredentialWithHTTP", func(t *testing.T) {
118+
clientOptions := &azopenai.ClientOptions{
119+
ClientOptions: policy.ClientOptions{
120+
InsecureAllowCredentialWithHTTP: true,
121+
},
122+
}
123+
124+
client, err := azopenai.NewClientForOpenAI(url, azcore.NewKeyCredential("fake-key"), clientOptions)
125+
require.NoError(t, err)
126+
127+
resp, err := client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
128+
Messages: []azopenai.ChatRequestMessageClassification{},
129+
}, nil)
130+
require.NoError(t, err)
131+
require.Equal(t, fakeID, *resp.ID)
132+
133+
client, err = azopenai.NewClientWithKeyCredential(url, azcore.NewKeyCredential("fake-key"), clientOptions)
134+
require.NoError(t, err)
135+
136+
resp, err = client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
137+
Messages: []azopenai.ChatRequestMessageClassification{},
138+
}, nil)
139+
require.NoError(t, err)
140+
require.Equal(t, fakeID, *resp.ID)
141+
142+
fakeCred := &credential.Fake{}
143+
144+
client, err = azopenai.NewClient(url, fakeCred, clientOptions)
145+
require.NoError(t, err)
146+
147+
resp, err = client.GetChatCompletions(context.Background(), azopenai.ChatCompletionsOptions{
148+
Messages: []azopenai.ChatRequestMessageClassification{},
149+
}, nil)
150+
require.NoError(t, err)
151+
require.Equal(t, fakeID, *resp.ID)
152+
})
153+
}

sdk/ai/azopenai/custom_client.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ func NewClient(endpoint string, credential azcore.TokenCredential, options *Clie
4444
options = &ClientOptions{}
4545
}
4646

47-
authPolicy := runtime.NewBearerTokenPolicy(credential, []string{tokenScope}, nil)
48-
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions)
47+
authPolicy := runtime.NewBearerTokenPolicy(credential, []string{tokenScope}, &policy.BearerTokenOptions{
48+
InsecureAllowCredentialWithHTTP: allowInsecure(options),
49+
})
50+
51+
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{
52+
PerRetry: []policy.Policy{authPolicy},
53+
}, &options.ClientOptions)
4954

5055
if err != nil {
5156
return nil, err
@@ -69,8 +74,13 @@ func NewClientWithKeyCredential(endpoint string, credential *azcore.KeyCredentia
6974
options = &ClientOptions{}
7075
}
7176

72-
authPolicy := runtime.NewKeyCredentialPolicy(credential, "api-key", nil)
73-
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions)
77+
authPolicy := runtime.NewKeyCredentialPolicy(credential, "api-key", &runtime.KeyCredentialPolicyOptions{
78+
InsecureAllowCredentialWithHTTP: allowInsecure(options),
79+
})
80+
81+
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{
82+
PerRetry: []policy.Policy{authPolicy},
83+
}, &options.ClientOptions)
7484
if err != nil {
7585
return nil, err
7686
}
@@ -95,11 +105,14 @@ func NewClientForOpenAI(endpoint string, credential *azcore.KeyCredential, optio
95105

96106
kp := runtime.NewKeyCredentialPolicy(credential, "authorization", &runtime.KeyCredentialPolicyOptions{
97107
Prefix: "Bearer ",
98-
InsecureAllowCredentialWithHTTP: options.InsecureAllowCredentialWithHTTP,
108+
InsecureAllowCredentialWithHTTP: allowInsecure(options),
99109
})
100110

101111
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{
102-
PerRetry: []policy.Policy{kp, newOpenAIPolicy()},
112+
PerRetry: []policy.Policy{
113+
kp,
114+
newOpenAIPolicy(),
115+
},
103116
}, &options.ClientOptions)
104117

105118
if err != nil {
@@ -308,3 +321,7 @@ func (c ChatRequestUserMessageContent) MarshalJSON() ([]byte, error) {
308321
func (c *ChatRequestUserMessageContent) UnmarshalJSON(data []byte) error {
309322
return json.Unmarshal(data, &c.value)
310323
}
324+
325+
func allowInsecure(options *ClientOptions) bool {
326+
return options != nil && options.InsecureAllowCredentialWithHTTP
327+
}

0 commit comments

Comments
 (0)