/// <summary> /// Configure <see cref="KubeClientOptions"/> from the settings specified in the <see cref="K8sConfig"/>. /// </summary> /// <param name="kubeClientOptions"> /// /// </param> /// <param name="kubeContextName"> /// The name of the Kubernetes context to use. /// /// If not specified, then the current context (as configured) will be used. /// </param> /// <param name="defaultKubeNamespace"> /// The default Kubernetes namespace to use. /// </param> /// <returns> /// The configured <see cref="KubeClientOptions"/>. /// </returns> public KubeClientOptions ConfigureKubeClientOptions(KubeClientOptions kubeClientOptions, string kubeContextName = null, string defaultKubeNamespace = null) { if (kubeClientOptions == null) { throw new ArgumentNullException(nameof(kubeClientOptions)); } string targetContextName = kubeContextName ?? CurrentContextName; if (String.IsNullOrWhiteSpace(targetContextName)) { throw new InvalidOperationException("The kubeContextName parameter was not specified, and the Kubernetes client configuration does not specify a current context."); } Context targetContext = Contexts.Find(context => context.Name == targetContextName); if (targetContext == null) { throw new InvalidOperationException($"Cannot find a context in the Kubernetes client configuration named '{targetContextName}'."); } Cluster targetCluster = Clusters.Find(cluster => cluster.Name == targetContext.Config.ClusterName); if (targetCluster == null) { throw new InvalidOperationException($"Cannot find a cluster in the Kubernetes client configuration named '{targetContext.Config.ClusterName}'."); } UserIdentity targetUser = UserIdentities.Find(user => user.Name == targetContext.Config.UserName); if (targetUser == null) { throw new InvalidOperationException($"Cannot find a user identity in the Kubernetes client configuration named '{targetContext.Config.UserName}'."); } kubeClientOptions.ApiEndPoint = new Uri(targetCluster.Config.Server); kubeClientOptions.KubeNamespace = defaultKubeNamespace; kubeClientOptions.ClientCertificate = targetUser.Config.GetClientCertificate(); kubeClientOptions.AllowInsecure = targetCluster.Config.AllowInsecure; kubeClientOptions.CertificationAuthorityCertificate = targetCluster.Config.GetCACertificate(); // Mixed authentication types are not supported. if (kubeClientOptions.ClientCertificate == null) { string accessToken = targetUser.Config.GetRawToken(); if (!String.IsNullOrWhiteSpace(accessToken)) { kubeClientOptions.AccessToken = accessToken; kubeClientOptions.AuthStrategy = KubeAuthStrategy.BearerToken; } else { kubeClientOptions.AuthStrategy = KubeAuthStrategy.None; } AuthProviderConfig authProvider = targetUser.Config.AuthProvider; if (authProvider != null) { kubeClientOptions.AuthStrategy = KubeAuthStrategy.BearerTokenProvider; if (authProvider.Config.TryGetValue("cmd-path", out object accessTokenCommand)) { kubeClientOptions.AccessTokenCommand = (string)accessTokenCommand; } if (authProvider.Config.TryGetValue("cmd-args", out object accessTokenCommandArguments)) { kubeClientOptions.AccessTokenCommandArguments = (string)accessTokenCommandArguments; } if (authProvider.Config.TryGetValue("token-key", out object accessTokenSelector)) { kubeClientOptions.AccessTokenSelector = (string)accessTokenSelector; } if (authProvider.Config.TryGetValue("expiry-key", out object accessTokenExpirySelector)) { kubeClientOptions.AccessTokenExpirySelector = (string)accessTokenExpirySelector; } if (authProvider.Config.TryGetValue("access-token", out object initialAccessToken)) { kubeClientOptions.InitialAccessToken = (string)initialAccessToken; } if (authProvider.Config.TryGetValue("expiry", out object initialTokenExpiry)) { kubeClientOptions.InitialTokenExpiryUtc = DateTime.Parse((string)initialTokenExpiry, provider: CultureInfo.InvariantCulture, styles: DateTimeStyles.AssumeUniversal ); } } } else { kubeClientOptions.AuthStrategy = KubeAuthStrategy.ClientCertificate; } return(kubeClientOptions); }
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> item) { var message = await item; // Save the message for later context.ConversationData.SetValue <Activity>("OriginalMessage", (Activity)message); // Let the user chose a provider var providers = AuthProviderConfig.GetAuthProviders(); PromptDialog.Choice(context, async(IDialogContext choiceContext, IAwaitable <AuthProviderConfig> choiceResult) => { var providerConfig = await choiceResult; choiceContext.ConversationData.SetValue <AuthProviderConfig>("AuthProvider", providerConfig); IAuthProvider authProvider; if (providerConfig.ProviderName == "Microsoft") { authProvider = new MSALAuthProvider(); } else { authProvider = new GenericOAuth2Provider($"GenericOAuth2Provider{providerConfig.ClientType}"); } await choiceContext.Forward(new AuthDialog(authProvider, providerConfig), async(IDialogContext authContext, IAwaitable <AuthResult> authResult) => { var result = await authResult; // Use token to call into service var prov = authContext.ConversationData.Get <AuthProviderConfig>("AuthProvider"); if (prov.ProviderName == "Microsoft") { var bytes = await new HttpClient().GetStreamWithAuthAsync(result.AccessToken, prov.PictureEndpoint); var pic = "data:image/png;base64," + Convert.ToBase64String(bytes); var m = authContext.MakeMessage(); m.Attachments.Add(new Attachment("image/png", pic)); await authContext.PostAsync(m); } else { var json = await new HttpClient().GetWithAuthAsync(result.AccessToken, prov.PictureEndpoint); var pic = ""; if (prov.ProviderName == "Google") { pic = json.Value <string>("picture"); } else if (prov.ProviderName == "Facebook") { pic = json.SelectToken("picture.data").Value <string>("url"); } else if (prov.ProviderName == "LinkedIn") { pic = json.Value <string>("pictureUrl"); } var m = authContext.MakeMessage(); m.Attachments.Add(new Attachment("image/png", pic)); await authContext.PostAsync(m); } // Wait for another message authContext.Wait(MessageReceivedAsync); }, choiceContext.ConversationData.Get <Activity>("OriginalMessage"), CancellationToken.None); }, providers, "Please select a provider we can use to look-up your information"); }