Skip to content
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

MinioAdminClient: add methods (attachPolicies, detachPolicies) #1626

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions adminapi/src/main/java/io/minio/admin/AttachPoliciesResp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2022 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.admin;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
import java.util.Arrays;

/** Represents detachPolicies information. */
@JsonIgnoreProperties(ignoreUnknown = true)
public class AttachPoliciesResp {
@JsonProperty("policiesAttached")
private String[] policiesAttached;

@JsonProperty("updatedAt")
private LocalDateTime updatedAt;

public String[] getPoliciesAttached() {
return policiesAttached;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

@Override
public String toString() {
return "AttachPoliciesResp{"
+ "policiesAttached="
+ Arrays.toString(policiesAttached)
+ ", updatedAt="
+ updatedAt
+ '}';
}
}
51 changes: 51 additions & 0 deletions adminapi/src/main/java/io/minio/admin/DetachPoliciesResp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2022 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.minio.admin;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
import java.util.*;

/** Represents detachPolicies information. */
@JsonIgnoreProperties(ignoreUnknown = true)
public class DetachPoliciesResp {
@JsonProperty("policiesDetached")
private String[] policiesDetached;

@JsonProperty("updatedAt")
private LocalDateTime updatedAt;

public String[] getPoliciesDetached() {
return policiesDetached;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

@Override
public String toString() {
return "DetachPoliciesResp{"
+ "policiesDetached="
+ Arrays.toString(policiesDetached)
+ ", updatedAt="
+ updatedAt
+ '}';
}
}
89 changes: 88 additions & 1 deletion adminapi/src/main/java/io/minio/admin/MinioAdminClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ private enum Command {
UPDATE_SERVICE_ACCOUNT("update-service-account"),
LIST_SERVICE_ACCOUNTS("list-service-accounts"),
DELETE_SERVICE_ACCOUNT("delete-service-account"),
INFO_SERVICE_ACCOUNT("info-service-account");
INFO_SERVICE_ACCOUNT("info-service-account"),
ATTACH_POLICIES("idp/builtin/policy/attach"),
DETACH_POLICIES("idp/builtin/policy/detach"),
;
private final String value;

private Command(String value) {
Expand Down Expand Up @@ -843,6 +846,90 @@ public GetServiceAccountInfoResp getServiceAccountInfo(@Nonnull String accessKey
}
}

/**
* Attach multiple policies to a user or group. <br>
*
* <p>Throws a runtime exception with the error code "XMinioAdminPolicyChangeAlreadyApplied" and
* message "The specified policy change is already in effect." if there is no difference between
* the policies before and after the change.
*
* @param userOrGroupName User or Group name.
* @param isGroup Flag to denote if userOrGroupName is a group name.
* @param policyNames Array of policy names to be attached.
* @throws NoSuchAlgorithmException Thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws InvalidKeyException Thrown to indicate missing of HMAC SHA-256 library.
* @throws IOException Thrown to indicate I/O error on MinIO REST operation.
*/
public AttachPoliciesResp attachPolicies(
@Nullable String userOrGroupName, boolean isGroup, String[] policyNames)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
InvalidCipherTextException {
if (userOrGroupName == null || userOrGroupName.isEmpty()) {
throw new IllegalArgumentException("user/group name must be provided");
}

Map<String, Object> policyEntity = new HashMap<>();
if (isGroup) {
policyEntity.put("group", userOrGroupName);
} else {
policyEntity.put("user", userOrGroupName);
}
policyEntity.put("policies", policyNames);

Credentials creds = getCredentials();
try (Response response =
execute(
Method.POST,
Command.ATTACH_POLICIES,
null,
Crypto.encrypt(OBJECT_MAPPER.writeValueAsBytes(policyEntity), creds.secretKey()))) {
byte[] jsonData = Crypto.decrypt(response.body().byteStream(), creds.secretKey());
return OBJECT_MAPPER.readValue(jsonData, AttachPoliciesResp.class);
}
}

/**
* Detach multiple policies to a user or group. <br>
*
* <p>Throws a runtime exception with the error code "XMinioAdminPolicyChangeAlreadyApplied" and
* message "The specified policy change is already in effect." if there is no difference between
* the policies before and after the change.
*
* @param userOrGroupName User or Group name.
* @param isGroup Flag to denote if userOrGroupName is a group name.
* @param policyNames Array of policy names to be attached.
* @throws NoSuchAlgorithmException Thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws InvalidKeyException Thrown to indicate missing of HMAC SHA-256 library.
* @throws IOException Thrown to indicate I/O error on MinIO REST operation.
*/
public DetachPoliciesResp detachPolicies(
@Nullable String userOrGroupName, boolean isGroup, String[] policyNames)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
InvalidCipherTextException {
if (userOrGroupName == null || userOrGroupName.isEmpty()) {
throw new IllegalArgumentException("user/group name must be provided");
}

Map<String, Object> policyEntity = new HashMap<>();
if (isGroup) {
policyEntity.put("group", userOrGroupName);
} else {
policyEntity.put("user", userOrGroupName);
}
policyEntity.put("policies", policyNames);

Credentials creds = getCredentials();
try (Response response =
execute(
Method.POST,
Command.DETACH_POLICIES,
null,
Crypto.encrypt(OBJECT_MAPPER.writeValueAsBytes(policyEntity), creds.secretKey()))) {
byte[] jsonData = Crypto.decrypt(response.body().byteStream(), creds.secretKey());
return OBJECT_MAPPER.readValue(jsonData, DetachPoliciesResp.class);
}
}

/**
* Sets HTTP connect, write and read timeouts. A value of 0 means no timeout, otherwise values
* must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
Expand Down