Skip to content

Commit a72e568

Browse files
szhGitHub Enterprise
authored andcommitted
Merge pull request #8 from Conjur-Enterprise/enable-tests
CNJR-7227 Fix and re-enable tests
2 parents 7ef9c3c + f2eadde commit a72e568

16 files changed

+290
-258
lines changed

.gitleaksignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/HostFactoryTest.cs:generic-api-key:21

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [3.0.3] - 2024-11-22
10+
11+
### Fixed
12+
- Resolve build warnings
13+
- Fix and re-enable broken tests (CNJR-7227)
14+
915
## [3.0.2] - 2024-08-02
1016

1117
### Changed

README.md

Lines changed: 101 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,90 +6,104 @@ invoking our Conjur API to perform operations on stored data (add, retrieve, etc
66

77
## Table of Contents
88

9-
- [Using this Project With Conjur Open Source](#Using-conjur-api-dotnet-with-Conjur-Open-Source)
9+
- [Using this Project With Conjur Open Source](#using-conjur-api-dotnet-with-conjur-open-source)
1010
- [Requirements](#requirements)
1111
- [Building](#building)
1212
- [Methods](#methods)
13-
- [Example](#example)
13+
- [Examples](#examples)
1414
- [Contributing](#contributing)
1515
- [License](#license)
1616

17-
## Using conjur-api-dotnet with Conjur Open Source
17+
## Using conjur-api-dotnet with Conjur Open Source
1818

19-
Are you using this project with [Conjur Open Source](https://github.com/cyberark/conjur)? Then we
20-
**strongly** recommend choosing the version of this project to use from the latest [Conjur OSS
21-
suite release](https://docs.conjur.org/Latest/en/Content/Overview/Conjur-OSS-Suite-Overview.html).
22-
Conjur maintainers perform additional testing on the suite release versions to ensure
23-
compatibility. When possible, upgrade your Conjur version to match the
24-
[latest suite release](https://docs.conjur.org/Latest/en/Content/ReleaseNotes/ConjurOSS-suite-RN.htm);
25-
when using integrations, choose the latest suite release that matches your Conjur version. For any
19+
Are you using this project with [Conjur Open Source](https://github.com/cyberark/conjur)? Then we
20+
**strongly** recommend choosing the version of this project to use from the latest [Conjur OSS
21+
suite release](https://docs.conjur.org/Latest/en/Content/Overview/Conjur-OSS-Suite-Overview.html).
22+
Conjur maintainers perform additional testing on the suite release versions to ensure
23+
compatibility. When possible, upgrade your Conjur version to match the
24+
[latest suite release](https://docs.conjur.org/Latest/en/Content/ReleaseNotes/ConjurOSS-suite-RN.htm);
25+
when using integrations, choose the latest suite release that matches your Conjur version. For any
2626
questions, please contact us on [Discourse](https://discuss.cyberarkcommons.org/c/conjur/5).
2727

2828
## Requirements
2929

30-
- Conjur Enterprise (formerly DAP) v10+ or Conjur Open Source v1+
31-
32-
For Conjur Enterprise V4, use the [V4 branch](https://github.com/cyberark/conjur-api-dotnet/tree/v4)
30+
- Conjur Enterprise v10+ or Conjur Open Source v1+
3331

3432
- When using the **AWS Authenticator**, Conjur Enterprise v13+ or Conjur Cloud (Conjur OSS was not tested)
3533

3634
## Building
3735

38-
This sample was built and tested with Visual Studio 2015.
36+
### Visual Studio
3937

4038
To load in Visual Studio, from the Visual Studio File menu select Open > Project/Solution > api-dotnet.sln
41-
and build the solution. This will create:
39+
and build the solution. This will create:
40+
41+
- conjur-api.dll: the .NET version of the Conjur API.
42+
- ConjurTest.dll: test DLL used for automated testing of the Conjur .NET API
43+
- example.exe: sample application that uses the Conjur API.
4244

43-
- conjur-api.dll: the .NET version of the Conjur API.
44-
- ConjurTest.dll: test DLL used for automated testing of the Conjur .NET API
45-
- example.exe: sample application that uses the Conjur API.
45+
### Docker
4646

47-
Optionally, to build in a Docker container, it is recommended to use Mono and xbuild.
47+
To build in a Docker container, run the following commands:
48+
49+
```bash
50+
make -C docker
51+
./build.sh
52+
```
4853

4954
## Methods
5055

5156
### `Client`
5257

5358
#### `Client Client(uri, account)`
59+
5460
- Create new Conjur instance
55-
- `uri` - URI of the Conjur server. Example: `https://myconjur.org.com/api`
56-
- `account` - Name of the Conjur account
61+
- `uri` - URI of the Conjur server. Example: `https://myconjur.org.com/api`
62+
- `account` - Name of the Conjur account
5763

5864
#### `void client.LogIn(string userName, string password)`
65+
5966
- Login to a Conjur user
60-
- `userName` - Username of Conjur user to login as
61-
- `password` - Password of user
67+
- `userName` - Username of Conjur user to login as
68+
- `password` - Password of user
6269

6370
#### `void client.TrustedCertificates.ImportPem (string certPath)`
71+
6472
- Add Conjur root certificate to system trust store
65-
- `certPath` = Path to cert
73+
- `certPath` = Path to cert
6674

6775
#### `void client.DisableCertCheck()`
76+
6877
- Disable SSL Cert check -- used when Conjur is configured with self-signed cert. Do not use in production.
6978

7079
#### `void client.EnableCertCheck()`
71-
- Enable SSL Cert check -- Default is to perform cert check; this method is used if there is a need to disable and enable the cert check.
7280

81+
- Enable SSL Cert check -- Default is to perform cert check; this method is used if there is a need to disable and enable the cert check.
7382

7483
#### `client.Credential = new NetworkCredential(string userName, string apiKey)`
84+
7585
- To login with an API key, use it directly
76-
- `userName` - Username of user to login as
77-
- `apiKey` - API key of user/host/etc
86+
- `userName` - Username of user to login as
87+
- `apiKey` - API key of user/host/etc
7888

7989
#### `IEnumerable<Variable> client.ListVariables(string query = null)`
90+
8091
- Returns a list of variable objects
81-
- `query` - Additional query parameters (not required)
92+
- `query` - Additional query parameters (not required)
8293

8394
#### `uint client.CountVariables(string query = null)`
95+
8496
- Return count of Conjur variables conforming to the `query` parameter
85-
- `query` - Additional query parameters (not required)
97+
- `query` - Additional query parameters (not required)
8698

8799
#### `Host client.CreateHost(string name, string hostFactoryToken)`
100+
88101
- Creates a host using a host factory token
89-
- `name` - Name of the host to create
90-
- `hostFactoryToken` - Host factory token
102+
- `name` - Name of the host to create
103+
- `hostFactoryToken` - Host factory token
91104

92105
#### `client.Authenticator = new Conjur.AWSIAMAuthenticator(Conjur.Client client, string Identity, string Authenticator, string roleArn = "", string ConjurAWSRegion = "us-east-1")`
106+
93107
- **REQUIREMENTS**: Conjur Enterprise v13+ or Conjur Cloud (Conjur OSS was not tested)
94108
- Configure the client to use the AWS IAM Authenticator
95109
- Client must be instantiated with these attributes before instantiating the AWS authenticator:
@@ -100,82 +114,88 @@ Optionally, to build in a Docker container, it is recommended to use Mono and xb
100114
### `Policy`
101115

102116
#### `Policy client.Policy(string policyName)`
103-
- Create a Conjur policy object
104-
- `policyName` - Name of policy
117+
118+
- Create a Conjur policy object
119+
- `policyName` - Name of policy
105120

106121
#### `policy.LoadPolicy(Stream policyContent)`
122+
107123
- Load policy into Conjur
108-
- `policyContent` - The policy
124+
- `policyContent` - The policy
109125

110126
### `Variable`
111127

112128
#### `Variable client.Variable(string name)`
129+
113130
- Instantiate a Variable object
114-
- `name` - Name of the variable
131+
- `name` - Name of the variable
115132

116133
#### `Boolean variable.Check(string privilege)`
134+
117135
- Check if the current entity has the specified privilege on this variable
118-
- `privilege` - string name of the privilege to check for
119-
- Privileges: read, create, update, delete, execute
136+
- `privilege` - string name of the privilege to check for
137+
- Privileges: read, create, update, delete, execute
120138

121139
#### `void variable.AddSecret(bytes val)`
140+
122141
- Change current variable to val
123-
- `val` - Value in bytes to update current variable to
142+
- `val` - Value in bytes to update current variable to
124143

125144
#### `String variable.GetValue()`
145+
126146
- Return the value of the current Variable
127147

128148
## Examples
129149

130-
#### Example Code
150+
### Example Code
131151

132152
```csharp
133-
// Instantiate a Conjur Client object.
134-
// parameter: URI - conjur appliance URI
135-
// parameter: ACCOUNT - conjur account name
136-
// return: Client object - if URI is incorrect errors thrown when used
137-
Client conjurClient = new Client("https://myorg.com", account);
138-
139-
// Login with Conjur credentials like userid and password,
140-
// or hostid and api_key, etc
141-
// parameters: username - conjur user or host id for example
142-
// password - conjur user password or host api key for example
143-
string conjurAuthToken = conjurClient.Login(username, password);
144-
145-
// Check if this user has permission to get the value of variableId
146-
// That requires execute permissions on the variable
147-
148-
// Instantiate a Variable object
149-
// parameters: client - contains authentication token and conjur URI
150-
// name - the name of the variable
151-
Variable conjurVariable = new Variable(conjurClient, variableId);
152-
153-
// Check if the current user has "execute" privilege required to get
154-
// the value of the variable
155-
// parameters: privilege - string name of the priv to check for
156-
bool isAllowed = conjurVariable.Check("execute");
157-
if (!isAllowed)
158-
{
159-
Console.WriteLine("You do not have permissions to get the value of {0}", variableId);
160-
}
161-
else
162-
{
163-
Console.WriteLine("{0} has the value: {1}", variableId, conjurVariable.GetValue());
164-
}
153+
// Instantiate a Conjur Client object.
154+
// parameter: URI - conjur appliance URI
155+
// parameter: ACCOUNT - conjur account name
156+
// return: Client object - if URI is incorrect errors thrown when used
157+
Client conjurClient = new Client("https://myorg.com", account);
158+
159+
// Login with Conjur credentials like userid and password,
160+
// or hostid and api_key, etc
161+
// parameters: username - conjur user or host id for example
162+
// password - conjur user password or host api key for example
163+
string conjurAuthToken = conjurClient.Login(username, password);
164+
165+
// Check if this user has permission to get the value of variableId
166+
// That requires execute permissions on the variable
167+
168+
// Instantiate a Variable object
169+
// parameters: client - contains authentication token and conjur URI
170+
// name - the name of the variable
171+
Variable conjurVariable = new Variable(conjurClient, variableId);
172+
173+
// Check if the current user has "execute" privilege required to get
174+
// the value of the variable
175+
// parameters: privilege - string name of the priv to check for
176+
bool isAllowed = conjurVariable.Check("execute");
177+
if (!isAllowed)
178+
{
179+
Console.WriteLine("You do not have permissions to get the value of {0}", variableId);
180+
}
181+
else
182+
{
183+
Console.WriteLine("{0} has the value: {1}", variableId, conjurVariable.GetValue());
184+
}
165185
```
166186

167-
#### Example App
187+
### Example App
168188

169189
This example app shows how to:
170190

171-
- Authenticate
172-
- Load Policy
173-
- Check permissions to get the value of a variable
174-
- Get the value of a variable
175-
- Use a Host Factory token to create a new Host and get an apiKey to use with Conjur
191+
- Authenticate
192+
- Load Policy
193+
- Check permissions to get the value of a variable
194+
- Get the value of a variable
195+
- Use a Host Factory token to create a new Host and get an apiKey to use with Conjur
176196

177197
To run the sample in Visual Studio, set the `example` project as the Startup
178-
Project. To do so, in
198+
Project. To do so, in
179199
the Solution Explorer right click over `example` and select `Set as Startup Project`.
180200

181201
```txt
@@ -191,7 +211,7 @@ Usage: Example <applianceURL>
191211
`applianceURL`: the applianceURL e.g. `https://conjur.myorg.com/`
192212

193213
`applianceCertificatePath`: the path and name of the Conjur appliance
194-
certificate. The easiest way to get the certifiate is to use the Conjur
214+
certificate. The easiest way to get the certifiate is to use the Conjur
195215
CLI command `conjur init -u conjur.myorg.com -f .conjurrc`. The certificate can be taken from any system you have run the Conjur CLI from.
196216

197217
`accountName`: The name of the account in Conjur.
@@ -203,9 +223,9 @@ CLI command `conjur init -u conjur.myorg.com -f .conjurrc`. The certificate can
203223
`variableId`: The name of an existing variable in Conjur that has a value set and for which the `username` has execute permissions.
204224

205225
`hostFactoryToken`: A host factory token. The easiest way to get a host
206-
factory token for testing is to add a hostfactory to a layer using
226+
factory token for testing is to add a hostfactory to a layer using
207227
the Conjur CLI command `conjur hostfactory create` and
208-
`conjur hostfactory token create`. Take the token returned from that call
228+
`conjur hostfactory token create`. Take the token returned from that call
209229
and pass it as the hostFactoryToken parameter to this example.
210230

211231
#### Example Code with AWS Authenticator

build.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ if [ ! -f "$PWD/VERSION" ]; then
1919
fi
2020

2121
CIDFILE=$(mktemp -u)
22-
docker run -v "$PWD":/src:ro --cidfile="$CIDFILE" -e WRITE_ARTIFACTORY_USERNAME -e WRITE_ARTIFACTORY_PASSWORD -e WRITE_ARTIFACTORY_URL "$TAG"
22+
docker run \
23+
-v "$PWD":/src:ro \
24+
--cidfile="$CIDFILE" \
25+
-e WRITE_ARTIFACTORY_USERNAME \
26+
-e WRITE_ARTIFACTORY_PASSWORD \
27+
-e WRITE_ARTIFACTORY_URL \
28+
-e RUN_AWS_TESTS \
29+
"$TAG"
2330

2431
CID=$(cat "$CIDFILE")
2532

conjur-api/AWSIAMAuthenticator.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ private Credentials GetSTSToken()
7272
// For now this can be handled by assuming the same role and allowing the AssumeRole permission for the role
7373
// for itself.
7474

75-
// TODO: Handle case where we're running on an EC2 instance that has the assigned role equal to the role
76-
// used to authenticate to Conjur. Then we don't need to assume a different role and we already have
77-
// the session token we need.
78-
// For now this can be handled by assuming the same role and allowing the AssumeRole permission for the role
79-
// for itself.
80-
8175
if (!String.IsNullOrEmpty(this.conjurIAMRole))
8276
{
8377
var assumeRoleReq = new AssumeRoleRequest()

conjur-api/ApiKeyAuthenticator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.IO;
1010
using System.Net;
1111
using System.Net.Http;
12-
using System.Runtime.CompilerServices;
1312
using System.Runtime.InteropServices;
1413
using System.Threading;
1514

@@ -26,6 +25,7 @@ public class ApiKeyAuthenticator : IAuthenticator
2625

2726
private string token = null;
2827
private Timer timer = null;
28+
private readonly HttpClient httpClient;
2929

3030
/// <summary>
3131
/// Initializes a new instance of the <see cref="Conjur.ApiKeyAuthenticator"/> class.
@@ -35,10 +35,11 @@ public class ApiKeyAuthenticator : IAuthenticator
3535
/// <param name="account">The name of the Conjur organization account.</param>
3636
/// <param name="credential">User name and API key to use, where
3737
/// username is for example "bob" or "host/jenkins".</param>
38-
public ApiKeyAuthenticator(Uri authnUri, string account, NetworkCredential credential)
38+
public ApiKeyAuthenticator(Uri authnUri, string account, NetworkCredential credential, HttpClient httpClient = null)
3939
{
4040
this.credential = credential;
4141
this.uri = new Uri($"{authnUri}/{Uri.EscapeDataString(account)}/{Uri.EscapeDataString(credential.UserName)}/authenticate");
42+
this.httpClient = httpClient ?? new HttpClient();
4243
}
4344

4445
#region IAuthenticator implementation
@@ -60,8 +61,6 @@ public string GetToken()
6061
{
6162
if (this.token == null)
6263
{
63-
HttpClient httpClient = new HttpClient();
64-
httpClient.Timeout = TimeSpan.FromMilliseconds(ApiConfigurationManager.GetInstance().HttpRequestTimeout);
6564
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, this.uri);
6665

6766
IntPtr bstr = IntPtr.Zero;
@@ -83,7 +82,7 @@ public string GetToken()
8382
stream.Headers.ContentLength = credential.SecurePassword.Length;
8483
httpRequestMessage.Content = stream;
8584

86-
var response = httpClient.Send(httpRequestMessage);
85+
var response = this.httpClient.Send(httpRequestMessage);
8786
response.EnsureSuccessStatusCode();
8887

8988
Interlocked.Exchange(ref this.token, response.Read());

conjur-api/Client.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public NetworkCredential Credential
9797
this.Authenticator = new ApiKeyAuthenticator(
9898
new Uri(this.ApplianceUri + "authn"),
9999
this.GetAccountName(),
100-
value);
100+
value,
101+
this.httpClient);
101102
}
102103
}
103104

0 commit comments

Comments
 (0)