-
Notifications
You must be signed in to change notification settings - Fork 16
Add list resources API support #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
miewest
wants to merge
2
commits into
cyberark:main
Choose a base branch
from
miewest:feature/list-resources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/com/cyberark/conjur/api/ConjurResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package com.cyberark.conjur.api; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Represents a Conjur resource returned by the | ||
| * <a href="https://docs.cyberark.com/conjur-open-source/latest/en/content/developer/conjur_api_list_resources.htm">List Resources</a> API. | ||
| * | ||
| * <p>A resource has an {@code id} in the form {@code {account}:{kind}:{identifier}}, | ||
| * along with metadata such as owner, policy, creation time, permissions, and annotations.</p> | ||
| */ | ||
| public class ConjurResource { | ||
|
|
||
| private String created_at; | ||
| private String id; | ||
| private String owner; | ||
| private String policy; | ||
| private List<Permission> permissions; | ||
| private List<Annotation> annotations; | ||
| private List<Map<String, Object>> secrets; | ||
| private List<Map<String, Object>> policy_versions; | ||
|
|
||
| /** @return the creation timestamp */ | ||
| public String getCreatedAt() { return created_at; } | ||
|
|
||
| /** | ||
| * The fully-qualified resource ID: {@code {account}:{kind}:{identifier}} | ||
| * @return the resource ID | ||
| */ | ||
| public String getId() { return id; } | ||
|
|
||
| /** @return the owner resource ID */ | ||
| public String getOwner() { return owner; } | ||
|
|
||
| /** @return the policy resource ID this resource belongs to */ | ||
| public String getPolicy() { return policy; } | ||
|
|
||
| /** @return the list of permissions on this resource */ | ||
| public List<Permission> getPermissions() { return permissions; } | ||
|
|
||
| /** @return the list of annotations on this resource */ | ||
| public List<Annotation> getAnnotations() { return annotations; } | ||
|
|
||
| /** @return secret version info (only for variables) */ | ||
| public List<Map<String, Object>> getSecrets() { return secrets; } | ||
|
|
||
| /** @return policy version info (only for policies) */ | ||
| public List<Map<String, Object>> getPolicyVersions() { return policy_versions; } | ||
|
|
||
| /** | ||
| * Extract the resource kind from the fully-qualified ID. | ||
| * For {@code myorg:variable:db/password}, returns {@code variable}. | ||
| * | ||
| * @return the kind portion of the ID, or null if the ID is malformed | ||
| */ | ||
| public String getKind() { | ||
| if (id == null) return null; | ||
| int first = id.indexOf(':'); | ||
| int second = id.indexOf(':', first + 1); | ||
| if (first < 0 || second < 0) return null; | ||
| return id.substring(first + 1, second); | ||
| } | ||
|
|
||
| /** | ||
| * Extract the identifier (without account and kind prefix) from the fully-qualified ID. | ||
| * For {@code myorg:variable:db/password}, returns {@code db/password}. | ||
| * | ||
| * @return the identifier portion of the ID, or null if the ID is malformed | ||
| */ | ||
| public String getIdentifier() { | ||
| if (id == null) return null; | ||
| int first = id.indexOf(':'); | ||
| int second = id.indexOf(':', first + 1); | ||
| if (first < 0 || second < 0) return null; | ||
| return id.substring(second + 1); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ConjurResource{id='" + id + "'}"; | ||
| } | ||
|
|
||
| /** | ||
| * Represents a permission entry on a resource. | ||
| */ | ||
| public static class Permission { | ||
| private String privilege; | ||
| private String role; | ||
| private String policy; | ||
|
|
||
| public String getPrivilege() { return privilege; } | ||
| public String getRole() { return role; } | ||
| public String getPolicy() { return policy; } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Permission{privilege='" + privilege + "', role='" + role + "'}"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Represents an annotation (key-value metadata) on a resource. | ||
| */ | ||
| public static class Annotation { | ||
| private String name; | ||
| private String value; | ||
| private String policy; | ||
|
|
||
| public String getName() { return name; } | ||
| public String getValue() { return value; } | ||
| public String getPolicy() { return policy; } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Annotation{name='" + name + "', value='" + value + "'}"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,61 +7,110 @@ | |
| import java.net.URI; | ||
|
|
||
| /** | ||
| * An <code>Endpoints</code> instance provides endpoint URIs for the various conjur services. | ||
| * An <code>Endpoints</code> instance provides endpoint URIs for the various Conjur services. | ||
| * | ||
| * <p>The canonical way to construct an {@code Endpoints} is from an appliance URL and account name. | ||
| * All service URIs are derived from these two values:</p> | ||
| * <ul> | ||
| * <li>Authentication: {@code {applianceUrl}/authn/{account}}</li> | ||
| * <li>Secrets (single): {@code {applianceUrl}/secrets/{account}/variable}</li> | ||
| * <li>Secrets (batch): {@code {applianceUrl}/secrets}</li> | ||
| * <li>Resources: {@code {applianceUrl}/resources/{account}}</li> | ||
| * </ul> | ||
| * | ||
| * <p>For non-standard authenticators (LDAP, OIDC, etc.), supply a custom authn URL.</p> | ||
| */ | ||
| public class Endpoints implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
| private final String applianceUrl; | ||
| private final String account; | ||
| private final URI authnUri; | ||
| private final URI secretsUri; | ||
|
|
||
| public Endpoints(final URI authnUri, final URI secretsUri){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Old constructors should stay as mentioned on the other PR for the batch secrets retrival: |
||
| this.authnUri = Args.notNull(authnUri, "authnUri"); | ||
| this.secretsUri = Args.notNull(secretsUri, "secretsUri"); | ||
| /** | ||
| * Create Endpoints from appliance URL and account, using standard authentication. | ||
| * | ||
| * @param applianceUrl the base Conjur appliance URL (e.g. {@code https://conjur.example.com}) | ||
| * @param account the Conjur account name (e.g. {@code conjur} for SaaS, or your org name) | ||
| */ | ||
| public Endpoints(String applianceUrl, String account) { | ||
| this(applianceUrl, account, applianceUrl + "/authn"); | ||
| } | ||
|
|
||
| public Endpoints(String authnUri, String secretsUri){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| this(URI.create(authnUri), URI.create(secretsUri)); | ||
| /** | ||
| * Create Endpoints from appliance URL, account, and a custom authentication URL. | ||
| * Use this when authenticating via a non-standard authenticator (LDAP, OIDC, etc.). | ||
| * | ||
| * @param applianceUrl the base Conjur appliance URL | ||
| * @param account the Conjur account name | ||
| * @param authnUrl the authentication service base URL | ||
| * (e.g. {@code https://conjur.example.com/authn-ldap/my-service}) | ||
| */ | ||
| public Endpoints(String applianceUrl, String account, String authnUrl) { | ||
| this.applianceUrl = Args.notNull(applianceUrl, "applianceUrl"); | ||
| this.account = Args.notNull(account, "account"); | ||
| this.authnUri = URI.create(String.format("%s/%s", authnUrl, account)); | ||
| this.secretsUri = URI.create(String.format("%s/secrets/%s/variable", applianceUrl, account)); | ||
| } | ||
|
|
||
| public URI getAuthnUri(){ return authnUri; } | ||
| public URI getAuthnUri() { return authnUri; } | ||
|
|
||
| public URI getSecretsUri() { | ||
| return secretsUri; | ||
| public URI getSecretsUri() { return secretsUri; } | ||
|
|
||
| public String getAccount() { return account; } | ||
|
|
||
| public String getApplianceUrl() { return applianceUrl; } | ||
|
|
||
| /** | ||
| * Returns the base URI for batch secret retrieval: {@code {applianceUrl}/secrets} | ||
| * | ||
| * @return the batch secrets URI | ||
| */ | ||
| public URI getBatchSecretsUri() { | ||
| return URI.create(applianceUrl + "/secrets"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the URI for listing resources: {@code {applianceUrl}/resources/{account}} | ||
| * | ||
| * @return the resources URI | ||
| */ | ||
| public URI getResourcesUri() { | ||
| return URI.create(String.format("%s/resources/%s", applianceUrl, account)); | ||
| } | ||
|
|
||
| public static Endpoints fromSystemProperties(){ | ||
| /** | ||
| * Create Endpoints from system properties / environment variables. | ||
| * Reads {@code CONJUR_ACCOUNT}, {@code CONJUR_APPLIANCE_URL}, and optionally {@code CONJUR_AUTHN_URL}. | ||
| */ | ||
| public static Endpoints fromSystemProperties() { | ||
| String account = Properties.getMandatoryProperty(Constants.CONJUR_ACCOUNT_PROPERTY); | ||
| String applianceUrl = Properties.getMandatoryProperty(Constants.CONJUR_APPLIANCE_URL_PROPERTY); | ||
| String authnUrl = Properties.getMandatoryProperty(Constants.CONJUR_AUTHN_URL_PROPERTY, applianceUrl + "/authn"); | ||
| String authnUrl = Properties.getMandatoryProperty( | ||
| Constants.CONJUR_AUTHN_URL_PROPERTY, applianceUrl + "/authn"); | ||
|
|
||
| return new Endpoints( | ||
| getAuthnServiceUri(authnUrl, account), | ||
| getServiceUri("secrets", account, "variable") | ||
| ); | ||
| return new Endpoints(applianceUrl, account, authnUrl); | ||
| } | ||
|
|
||
| public static Endpoints fromCredentials(Credentials credentials){ | ||
| /** | ||
| * Create Endpoints using the authentication URL from the given credentials. | ||
| * Account and appliance URL are read from system properties / environment variables. | ||
| */ | ||
| public static Endpoints fromCredentials(Credentials credentials) { | ||
| String account = Properties.getMandatoryProperty(Constants.CONJUR_ACCOUNT_PROPERTY); | ||
| return new Endpoints( | ||
| getAuthnServiceUri(credentials.getAuthnUrl(), account), | ||
| getServiceUri("secrets", account, "variable") | ||
| ); | ||
| } | ||
|
|
||
| private static URI getAuthnServiceUri(String authnUrl, String accountName) { | ||
| return URI.create(String.format("%s/%s", authnUrl, accountName)); | ||
| } | ||
| String applianceUrl = Properties.getMandatoryProperty(Constants.CONJUR_APPLIANCE_URL_PROPERTY); | ||
|
|
||
| private static URI getServiceUri(String service, String accountName, String path){ | ||
| return URI.create(String.format("%s/%s/%s/%s", Properties.getMandatoryProperty(Constants.CONJUR_APPLIANCE_URL_PROPERTY), service, accountName, path)); | ||
| return new Endpoints(applianceUrl, account, credentials.getAuthnUrl()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Endpoints{" + | ||
| "authnUri=" + authnUri + | ||
| "secretsUri=" + secretsUri + | ||
| "applianceUrl=" + applianceUrl + | ||
| ", account=" + account + | ||
| ", authnUri=" + authnUri + | ||
| ", secretsUri=" + secretsUri + | ||
| '}'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the methods seem to be unnecessary shortcuts. These should be accessed through for example
variables