static int SearchSecretId(SecretServerClient client, string uri, string template, string name) { Log.Debug("Using Rest API to search secrets."); var httpClient = new HttpClient(); var uriBuilder = new UriBuilder(uri) { Path = "/api/v1/secrets", Query = $"secretTemplateName={template}&filter.searchText={name}" }; Log.Debug($"Secret Server Search Uri: {uriBuilder.Uri.AbsoluteUri}"); // Would be really nice if the SDK had a function to do this: var requestMessage = new HttpRequestMessage(HttpMethod.Get, uriBuilder.Uri.AbsoluteUri); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", client.GetAccessToken()); var result = httpClient.SendAsync(requestMessage).Result; var secretResponse = result.Content.ReadAsStringAsync().Result; var secretRecords = JsonConvert.DeserializeObject <PagingOfSecretSummary>(secretResponse); var secretId = secretRecords.Records[0].Id; Log.Debug($"Retrieving Secret Id '{secretId}' from Secret Server."); return(secretId); }
public ISecretServerClient GetClient(SecretServerContext context) { ConfigurationManager.AppSettings["SecretServerSdkConfigDirectory"] = context.SdkConfigDirectory; var client = new SecretServerClient(); client.Configure(new ConfigSettings { SecretServerUrl = context.SecretServerUrl, RuleName = context.RuleName, RuleKey = context.RuleKey, CacheStrategy = CacheStrategy.Never, ResetToken = context.ResetToken, }); return(client); }
static void DemonstrateSdkFunctionality(IConfigurationSection config) { Log.Debug("Thycotic Secret Server Constrcutor"); var client = new SecretServerClient(); Log.Debug("Loading Configuration from Config Section"); var sdkConfig = new { Uri = config.GetValue <string>("Uri"), RuleName = config.GetValue <string>("RuleName"), RuleKey = config.GetValue <string>("RuleKey"), CacheAge = config.GetValue <int>("CacheAge"), ResetToken = Path.GetRandomFileName().Replace(".", string.Empty) }; Log.Debug($"Loaded Configuration: \n{JsonConvert.SerializeObject(sdkConfig, Formatting.Indented)}"); Log.Debug("Configuring Thycotic SecretServerClient"); client.Configure(new ConfigSettings { SecretServerUrl = sdkConfig.Uri, RuleName = sdkConfig.RuleName, RuleKey = sdkConfig.RuleKey, CacheStrategy = CacheStrategy.CacheThenServerAllowExpired, CacheAge = sdkConfig.CacheAge, ResetToken = sdkConfig.ResetToken }); Log.Debug("Thycotic Secret Server Client Initialized"); var templateName = config.GetValue <string>("SecretTemplateName"); var searchText = config.GetValue <string>("SearchText"); var secretId = SearchSecretId(client, sdkConfig.Uri, templateName, searchText); var secret = client.GetSecret(secretId); foreach (var item in secret.Items) { Log.Debug($"{item.Slug}: {item.ItemValue}"); } }