Skip to content

Commit

Permalink
stylecop & license update
Browse files Browse the repository at this point in the history
  • Loading branch information
uCatu authored and JakeQuilty committed May 22, 2020
1 parent 197518a commit a120722
Show file tree
Hide file tree
Showing 29 changed files with 232 additions and 306 deletions.
70 changes: 26 additions & 44 deletions conjur-api/ApiConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// <copyright file="ApiConfigurationManager.cs" company="Conjur Inc.">
// Copyright (c) 2016 Conjur Inc. All rights reserved.
// <copyright file="ApiConfigurationManager.cs" company="CyberArk Software Ltd.">
// Copyright (c) 2020 CyberArk Software Ltd. All rights reserved.
// </copyright>
// <summary>
// Configuration class for this API project
// </summary>
using System;
using System.Configuration;

namespace Conjur
{
using System;
using System.Configuration;

public sealed class ApiConfigurationManager
{
public const string HTTP_REQUEST_TIMEOUT = "HTTP_REQUEST_TIMEOUT";
Expand All @@ -31,64 +31,46 @@ public static ApiConfigurationManager GetInstance()
/// Gets/Sets the global http request timeout configuration.
/// Request timeout configuration in milliseconds, default is: 100000.
/// </summary>
public int HttpRequestTimeout
{
get
{
if (httpRequestTimeout == null)
{
lock (locker)
{
if (httpRequestTimeout == null)
{
string value = ConfigurationManager.AppSettings.Get(HTTP_REQUEST_TIMEOUT);
public int HttpRequestTimeout {
get {
if (httpRequestTimeout == null) {
lock (locker) {
if (httpRequestTimeout == null) {
string value = ConfigurationManager.AppSettings.Get (HTTP_REQUEST_TIMEOUT);
httpRequestTimeout = 100000; //100 seconds; WebRequest default timeout
if (!string.IsNullOrWhiteSpace(value))
{
httpRequestTimeout = Convert.ToInt32(value);
if (!string.IsNullOrWhiteSpace (value)) {
httpRequestTimeout = Convert.ToInt32 (value);
}
}
}
}
return httpRequestTimeout.Value;
}
set
{
httpRequestTimeout = value;
}
set => httpRequestTimeout = value;
}

private uint? tokenRefreshTimeout = null;

/// <summary>
/// Gets/Sets the global token refresh timeout configuration.
/// Token refresh timeout configuration in milliseconds, default is: 450000 (7 minutes 30 seconds).
/// Token refresh timeout configuration in milliseconds, default is: 420000 (7 minutes).
/// </summary>
public uint TokenRefreshTimeout
{
get
{
if (tokenRefreshTimeout == null)
{
lock (locker)
{
if (tokenRefreshTimeout == null)
{
string value = ConfigurationManager.AppSettings.Get(TOKEN_REFRESH_TIMEOUT);
tokenRefreshTimeout = 450000; //7 minutes 30 seconds
if (!string.IsNullOrWhiteSpace(value))
{
tokenRefreshTimeout = Convert.ToUInt32(value);
public uint TokenRefreshTimeout {
get {
if (tokenRefreshTimeout == null) {
lock (locker) {
if (tokenRefreshTimeout == null) {
string value = ConfigurationManager.AppSettings.Get (TOKEN_REFRESH_TIMEOUT);
tokenRefreshTimeout = 420000; //7 minutes
if (!string.IsNullOrWhiteSpace (value)) {
tokenRefreshTimeout = Convert.ToUInt32 (value);
}
}
}
}
return tokenRefreshTimeout.Value;
}
set
{
tokenRefreshTimeout = value;
}
set => tokenRefreshTimeout = value;
}

private class Nested
Expand All @@ -100,4 +82,4 @@ static Nested()
}
}
}
}
}
16 changes: 8 additions & 8 deletions conjur-api/ApiKeyAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// <copyright file="ApiKeyAuthenticator.cs" company="Conjur Inc.">
// Copyright (c) 2016 Conjur Inc. All rights reserved.
// <copyright file="ApiKeyAuthenticator.cs" company="CyberArk Software Ltd.">
// Copyright (c) 2020 CyberArk Software Ltd. All rights reserved.
// </copyright>
// <summary>
// API key authenticator.
// </summary>

using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;

namespace Conjur
{
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;

/// <summary>
/// API key authenticator.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions conjur-api/Client.Methods.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// <copyright file="Client.Methods.cs" company="Conjur Inc.">
// <copyright file="Client.Methods.cs "company="CyberArk Software Ltd.">
// Copyright (c) 2020 CyberArk Software Ltd. All rights reserved.
// </copyright>
// <summary>
// Conjur Client methods delegating to entity-specific classes.
// </summary>
using System.Collections.Generic;

namespace Conjur
{
using System.Collections.Generic;

/// <summary>
/// Entity-specific methods for the Client facade.
/// </summary>
Expand Down Expand Up @@ -120,4 +120,4 @@ public Client ActingAs(string role)
return new Client(this, role);
}
}
}
}
47 changes: 18 additions & 29 deletions conjur-api/Client.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// <copyright file="Client.cs" company="Conjur Inc.">
// <copyright file="Client.cs" company="CyberArk Software Ltd.">
// Copyright (c) 2020 CyberArk Software Ltd. All rights reserved.
// </copyright>
// <summary>
// Base Conjur client class implementation.
// </summary>

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;

namespace Conjur
{
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// Conjur API client.
/// </summary>
public partial class Client
{
private Uri applianceUri;
private string account;
private string actingAs;
private readonly string account;
private readonly string actingAs;

/// <summary>
/// Initializes a new instance of the <see cref="T:Conjur.Client"/> class.
Expand All @@ -31,7 +30,7 @@ public partial class Client
public Client(string applianceUri, string account)
{
this.account = account;
this.applianceUri = NormalizeBaseUri(applianceUri);
this.ApplianceUri = NormalizeBaseUri(applianceUri);
this.TrustedCertificates = new X509Certificate2Collection();
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(this.ValidateCertificate);
Expand All @@ -47,13 +46,7 @@ internal Client(Client other, string role) : this(other.ApplianceUri.AbsoluteUri
/// Gets the appliance URI.
/// </summary>
/// <value>The appliance URI.</value>
public Uri ApplianceUri
{
get
{
return this.applianceUri;
}
}
public Uri ApplianceUri { get; }

/// <summary>
/// Gets or sets the authenticator used to establish Conjur identity.
Expand All @@ -69,15 +62,11 @@ public Uri ApplianceUri
/// </summary>
/// <value>The credential of user name and API key, where user name is
/// for example "bob" or "host/jenkins".</value>
public NetworkCredential Credential
{
set
{
this.Authenticator = new ApiKeyAuthenticator(
new Uri(this.applianceUri + "authn"),
this.GetAccountName(),
public NetworkCredential Credential {
set => this.Authenticator = new ApiKeyAuthenticator (
new Uri (this.ApplianceUri + "authn"),
this.GetAccountName (),
value);
}
}

/// <summary>
Expand Down Expand Up @@ -140,7 +129,7 @@ public string LogIn(NetworkCredential credential)
/// <returns>A WebRequest for the specified appliance path.</returns>
public WebRequest Request(string path)
{
WebRequest reqest = WebRequest.Create(this.applianceUri + path);
WebRequest reqest = WebRequest.Create(this.ApplianceUri + path);
reqest.Timeout = ApiConfigurationManager.GetInstance().HttpRequestTimeout;
return reqest;
}
Expand Down Expand Up @@ -218,4 +207,4 @@ private WebRequest ApplyAuthentication(WebRequest webRequest)
return webRequest;
}
}
}
}
4 changes: 2 additions & 2 deletions conjur-api/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="Constants.cs" company="Conjur Inc.">
// <copyright file="Constants.cs" company="CyberArk Software Ltd.">
// Copyright (c) 2020 CyberArk Software Ltd. All rights reserved.
// </copyright>
// <summary>
Expand All @@ -16,4 +16,4 @@ public class Constants
public const string KIND_VARIABLE = "variable";
public const string KIND_WEBSERVICE = "webservice";
}
}
}
40 changes: 20 additions & 20 deletions conjur-api/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// <copyright file="Extensions.cs" company="Conjur Inc.">
// Copyright (c) 2016 Conjur Inc. All rights reserved.
// <copyright file="Extensions.cs" company="CyberArk Software Ltd.">
// Copyright (c) 2020 CyberArk Software Ltd. All rights reserved.
// </copyright>
// <summary>
// Utility extension methods.
// </summary>
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;

namespace Conjur
{
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;

/// <summary>
/// Utility extension methods.
/// </summary>
Expand All @@ -28,7 +28,7 @@ public static class Extensions
{
const string HEADER = "-----BEGIN CERTIFICATE-----";
const string FOOTER = "-----END CERTIFICATE-----";
var re = new Regex(HEADER + "(.*?)" + FOOTER, RegexOptions.Singleline);
Regex re = new Regex(HEADER + "(.*?)" + FOOTER, RegexOptions.Singleline);
foreach (Match match in re.Matches(File.ReadAllText(fileName)))
{
collection.Import(Convert.FromBase64String(match.Groups[1].Value));
Expand All @@ -42,9 +42,10 @@ public static class Extensions
/// <param name="request">Request to read from.</param>
internal static string Read(this WebRequest request)
{
using (var reader
= new StreamReader(request.GetResponse().GetResponseStream()))
return reader.ReadToEnd();
using (StreamReader reader
= new StreamReader (request.GetResponse ().GetResponseStream ())) {
return reader.ReadToEnd ();
}
}

internal static bool VerifyWithExtraRoots(
Expand All @@ -53,30 +54,29 @@ internal static string Read(this WebRequest request)
X509Certificate2Collection extraRoots)
{
chain.ChainPolicy.ExtraStore.AddRange(extraRoots);
if (chain.Build(new X509Certificate2(certificate)))
if (chain.Build(new X509Certificate2(certificate))) {
return true;
else
{
} else {
// .NET returns UntrustedRoot status flag if the certificate is not in
// the SYSTEM trust store. Check if it's the only problem with the chain.
var onlySystemUntrusted =
bool onlySystemUntrusted =
chain.ChainStatus.Length == 1 &&
chain.ChainStatus[0].Status == X509ChainStatusFlags.UntrustedRoot;

// Sanity check that indeed that is the only problem with the root
// certificate.
var rootCert = chain.ChainElements[chain.ChainElements.Count - 1];
var rootOnlySystemUntrusted =
X509ChainElement rootCert = chain.ChainElements[chain.ChainElements.Count - 1];
bool rootOnlySystemUntrusted =
rootCert.ChainElementStatus.Length == 1 &&
rootCert.ChainElementStatus[0].Status
== X509ChainStatusFlags.UntrustedRoot;

// Double check it's indeed one of the extra roots we've been given.
var rootIsUserTrusted = extraRoots.Contains(rootCert.Certificate);
bool rootIsUserTrusted = extraRoots.Contains(rootCert.Certificate);

return
onlySystemUntrusted && rootOnlySystemUntrusted && rootIsUserTrusted;
}
}
}
}
}
Loading

0 comments on commit a120722

Please sign in to comment.