Skip to content

Commit

Permalink
fix the keyCredential issue (microsoft#4412)
Browse files Browse the repository at this point in the history
Fixes microsoft#4411
This change gives the azure plugin the ability to change the type of the
credentials, but it will still have to override everything in
ClientProvider to use the token credential.
Maybe only a credential abstraction change could solve it, but it needs
more discussions.
  • Loading branch information
ArcturusZhang authored Sep 12, 2024
1 parent e62e58a commit f9ae0a1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ internal static class ScmKnownParameters
public static readonly ParameterProvider Data = new("data", FormattableStringHelpers.Empty, typeof(BinaryData));
public static ParameterProvider ClientOptions(CSharpType clientOptionsType)
=> new("options", $"The options for configuring the client.", clientOptionsType.WithNullable(true), initializationValue: New.Instance(clientOptionsType.WithNullable(true)));
public static readonly ParameterProvider TokenAuth = new("tokenCredential", $"The token credential to copy", ClientModelPlugin.Instance.TypeFactory.TokenCredentialType());
public static readonly ParameterProvider MatchConditionsParameter = new("matchConditions", $"The content to send as the request conditions of the request.", ClientModelPlugin.Instance.TypeFactory.MatchConditionsType(), DefaultOf(ClientModelPlugin.Instance.TypeFactory.MatchConditionsType()));
public static readonly ParameterProvider KeyAuth = new("keyCredential", $"The token credential to copy", ClientModelPlugin.Instance.TypeFactory.KeyCredentialType);
public static readonly ParameterProvider MatchConditionsParameter = new("matchConditions", $"The content to send as the request conditions of the request.", ClientModelPlugin.Instance.TypeFactory.MatchConditionsType, DefaultOf(ClientModelPlugin.Instance.TypeFactory.MatchConditionsType));
public static readonly ParameterProvider RequestOptions = new("options", $"The request options, which can override default behaviors of the client pipeline on a per-call basis.", typeof(RequestOptions));
public static readonly ParameterProvider BinaryContent = new("content", $"The content to send as the body of the request.", typeof(BinaryContent), location: ParameterLocation.Body) { Validation = ParameterValidationType.AssertNotNull };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ClientProvider(InputClient inputClient)
var apiKey = _inputAuth?.ApiKey;
_apiKeyAuthField = apiKey != null ? new FieldProvider(
FieldModifiers.Private | FieldModifiers.ReadOnly,
typeof(ApiKeyCredential),
ClientModelPlugin.Instance.TypeFactory.KeyCredentialType,
ApiKeyCredentialFieldName,
this,
description: $"A credential used to authenticate to the service.") : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
Expand All @@ -16,9 +17,11 @@ public class ScmTypeFactory : TypeFactory
private Dictionary<InputClient, ClientProvider>? _clientCache;
private Dictionary<InputClient, ClientProvider> ClientCache => _clientCache ??= [];

public virtual CSharpType MatchConditionsType() => typeof(PipelineMessageClassifier);
public virtual CSharpType MatchConditionsType => typeof(PipelineMessageClassifier);

public virtual CSharpType TokenCredentialType() => typeof(ApiKeyCredential);
public virtual CSharpType KeyCredentialType => typeof(ApiKeyCredential);

public virtual CSharpType TokenCredentialType => throw new NotImplementedException("Token credential is not supported in Scm libraries yet");

/// <summary>
/// Returns the serialization type providers for the given input type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal class ScmKnownParametersTests
[Test]
public void TestTokenAuth()
{
MockHelpers.LoadMockPlugin(tokenCredentialType: () => typeof(int));
MockHelpers.LoadMockPlugin(keyCredentialType: () => typeof(int));

var result = ClientModelPlugin.Instance.TypeFactory.TokenCredentialType();
var result = ClientModelPlugin.Instance.TypeFactory.KeyCredentialType;
Assert.IsNotNull(result);
Assert.AreEqual(new CSharpType(typeof(int)), result);
}
Expand All @@ -23,7 +23,7 @@ public void TestMatchConditionsParameter()
{
MockHelpers.LoadMockPlugin(matchConditionsType: () => typeof(int));

var result = ClientModelPlugin.Instance.TypeFactory.MatchConditionsType();
var result = ClientModelPlugin.Instance.TypeFactory.MatchConditionsType;
Assert.IsNotNull(result);
Assert.AreEqual(new CSharpType(typeof(int)), result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static Mock<ClientModelPlugin> LoadMockPlugin(
Func<InputType, TypeProvider, IReadOnlyList<TypeProvider>>? createSerializationsCore = null,
Func<InputType, CSharpType>? createCSharpTypeCore = null,
Func<CSharpType>? matchConditionsType = null,
Func<CSharpType>? tokenCredentialType = null,
Func<CSharpType>? keyCredentialType = null,
Func<InputParameter, ParameterProvider>? createParameterCore = null,
Func<InputApiKeyAuth>? apiKeyAuth = null,
Func<IReadOnlyList<string>>? apiVersions = null,
Expand Down Expand Up @@ -53,12 +53,12 @@ public static Mock<ClientModelPlugin> LoadMockPlugin(

if (matchConditionsType is not null)
{
mockTypeFactory.Setup(p => p.MatchConditionsType()).Returns(matchConditionsType);
mockTypeFactory.Setup(p => p.MatchConditionsType).Returns(matchConditionsType);
}

if (tokenCredentialType is not null)
if (keyCredentialType is not null)
{
mockTypeFactory.Setup(p => p.TokenCredentialType()).Returns(tokenCredentialType);
mockTypeFactory.Setup(p => p.KeyCredentialType).Returns(keyCredentialType);
}

if (createParameterCore is not null)
Expand Down

0 comments on commit f9ae0a1

Please sign in to comment.