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

Support updates of API key attributes [service layer] #87924

Merged
merged 113 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 108 commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
3bba543
Service and request draft
n1v0lg Jun 21, 2022
3a709a9
Update response
n1v0lg Jun 21, 2022
fc53cf3
Get api key docs for name
n1v0lg Jun 21, 2022
74d264c
More
n1v0lg Jun 21, 2022
0bc2ec5
More
n1v0lg Jun 21, 2022
648ae52
Check expiration
n1v0lg Jun 21, 2022
2d43dda
Integ test
n1v0lg Jun 21, 2022
4384e56
More
n1v0lg Jun 21, 2022
3d73b99
Assert role descriptors as expected
n1v0lg Jun 21, 2022
ff54fec
Clean up role descriptor checks
n1v0lg Jun 21, 2022
e484923
Test not found
n1v0lg Jun 21, 2022
a9c8caa
WIP expected metadata
n1v0lg Jun 21, 2022
d638a45
Fix metadata
n1v0lg Jun 21, 2022
0c819e4
Nit
n1v0lg Jun 21, 2022
abe677b
Fix setting metadata
n1v0lg Jun 21, 2022
3c7b2c1
Nit
n1v0lg Jun 21, 2022
29f0320
WIP use old role descriptors
n1v0lg Jun 21, 2022
eef2b38
Clean up
n1v0lg Jun 21, 2022
e5b8650
More todos
n1v0lg Jun 21, 2022
30cfc39
One more
n1v0lg Jun 21, 2022
b14271a
Other users API key not found
n1v0lg Jun 21, 2022
cd5d58f
Nit
n1v0lg Jun 21, 2022
eeca38a
Tweaks
n1v0lg Jun 21, 2022
7c3d3f0
Null bytes
n1v0lg Jun 21, 2022
f6d1783
Other
n1v0lg Jun 21, 2022
0e44586
Not found test clean up
n1v0lg Jun 22, 2022
3c83743
Tweaks
n1v0lg Jun 22, 2022
b88cdc8
Inactive tests
n1v0lg Jun 22, 2022
68eaa91
Expiration test
n1v0lg Jun 22, 2022
36bb431
Friendlier exception message
n1v0lg Jun 22, 2022
c545e20
Translate response
n1v0lg Jun 22, 2022
63c0729
Noop not possible
n1v0lg Jun 22, 2022
7aefeb1
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 22, 2022
89d7229
Much clean up
n1v0lg Jun 22, 2022
003f969
Test validate
n1v0lg Jun 22, 2022
8937d94
Spotless
n1v0lg Jun 22, 2022
bdd4dfb
Also test empty name
n1v0lg Jun 22, 2022
4bd5cad
More clean up
n1v0lg Jun 22, 2022
d88f949
Skip todo
n1v0lg Jun 22, 2022
421e04f
Remove unused
n1v0lg Jun 22, 2022
719e4ba
Update request tests and serialization
n1v0lg Jun 22, 2022
c536534
Lint
n1v0lg Jun 22, 2022
c46ab34
Clean up
n1v0lg Jun 22, 2022
de179dd
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 22, 2022
7931e00
Nits
n1v0lg Jun 22, 2022
3f8b218
Checkstyle
n1v0lg Jun 22, 2022
7166217
Fix
n1v0lg Jun 22, 2022
0f48e8c
Owner realms
n1v0lg Jun 22, 2022
ae573ca
Checkstyle
n1v0lg Jun 22, 2022
629e5a1
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 23, 2022
d29c1e2
Use raw field
n1v0lg Jun 23, 2022
147adb0
Clean up new and merged doc
n1v0lg Jun 23, 2022
8cffe22
With versioning
n1v0lg Jun 23, 2022
0cac5e5
Clean up
n1v0lg Jun 23, 2022
7d9956e
Invalidate doc cache
n1v0lg Jun 23, 2022
f5d089f
More clean up
n1v0lg Jun 23, 2022
0b34c7b
Test cache
n1v0lg Jun 23, 2022
65c8d46
Assert auth cache not cleared
n1v0lg Jun 23, 2022
2e3b623
Null tweaks
n1v0lg Jun 23, 2022
2f91430
Null input stream
n1v0lg Jun 23, 2022
73d3fac
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 24, 2022
ef49292
Updated doc test
n1v0lg Jun 24, 2022
3f099c4
Lint
n1v0lg Jun 24, 2022
9426e56
More lint
n1v0lg Jun 24, 2022
325d4d0
More clean up
n1v0lg Jun 24, 2022
044831f
Put in version stop gap
n1v0lg Jun 24, 2022
1d3d8a2
Combine tests
n1v0lg Jun 24, 2022
e377155
Include domain
n1v0lg Jun 24, 2022
e95fd34
Use param message
n1v0lg Jun 24, 2022
c536811
Bump version
n1v0lg Jun 24, 2022
b7b4c36
Check get api correct metadata
n1v0lg Jun 24, 2022
30b0267
Rename
n1v0lg Jun 24, 2022
17375b3
Still the logging
n1v0lg Jun 24, 2022
0481c01
Imports
n1v0lg Jun 24, 2022
1a29f8b
Test creator
n1v0lg Jun 24, 2022
6d7a341
Add creator assertions
n1v0lg Jun 24, 2022
99d5d06
Randomize expiration
n1v0lg Jun 24, 2022
6d7013c
Fix assertion
n1v0lg Jun 24, 2022
64990b5
WIP tests multiple role descriptors
n1v0lg Jun 24, 2022
bba8f2b
More clean up
n1v0lg Jun 24, 2022
dd69a00
Support multiple roles
n1v0lg Jun 24, 2022
3aefef8
Domain check and test clean up
n1v0lg Jun 24, 2022
49b4dd5
Better names
n1v0lg Jun 26, 2022
4a0b2b2
WIP address feedback
n1v0lg Jun 27, 2022
2bd3ea4
Scroll helper
n1v0lg Jun 27, 2022
0473c5d
Add assertion
n1v0lg Jun 27, 2022
6c33703
api key error message
n1v0lg Jun 27, 2022
d7bd723
More inline
n1v0lg Jun 27, 2022
9418b9b
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 27, 2022
0b2457d
Single and fix test
n1v0lg Jun 27, 2022
3f283d9
Better error message
n1v0lg Jun 27, 2022
0d02eba
Nit
n1v0lg Jun 27, 2022
82fdb6b
Api keys not allowed
n1v0lg Jun 27, 2022
7a371a4
Move assertion up
n1v0lg Jun 27, 2022
4586139
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 27, 2022
910500b
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 27, 2022
a9d005f
Error message
n1v0lg Jun 27, 2022
49eba62
Nit
n1v0lg Jun 27, 2022
58a7e02
Assert
n1v0lg Jun 27, 2022
3946dc1
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
a896ae0
Address feedback
n1v0lg Jun 28, 2022
542b360
More logs and finals
n1v0lg Jun 28, 2022
7c36b17
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
b9fcad1
More randomization in test
n1v0lg Jun 28, 2022
2b45045
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
87b6fb0
Caps API key
n1v0lg Jun 28, 2022
725248d
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
7481d52
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
ca65709
Address review
n1v0lg Jun 28, 2022
e025390
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
60d6ce4
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
d35d323
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 28, 2022
88718ea
Merge branch 'master' into update-api-keys-service-level
n1v0lg Jun 29, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.core.security.action.apikey;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.xpack.core.security.action.role.RoleDescriptorRequestValidator;
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
import org.elasticsearch.xpack.core.security.support.MetadataUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.action.ValidateActions.addValidationError;

public final class UpdateApiKeyRequest extends ActionRequest {

private final String id;
@Nullable
private final Map<String, Object> metadata;
@Nullable
private final List<RoleDescriptor> roleDescriptors;

public UpdateApiKeyRequest(String id, @Nullable List<RoleDescriptor> roleDescriptors, @Nullable Map<String, Object> metadata) {
this.id = Objects.requireNonNull(id, "API key ID must not be null");
this.roleDescriptors = roleDescriptors;
this.metadata = metadata;
}

public UpdateApiKeyRequest(StreamInput in) throws IOException {
super(in);
this.id = in.readString();
this.roleDescriptors = readOptionalList(in);
this.metadata = in.readMap();
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (metadata != null && MetadataUtils.containsReservedMetadata(metadata)) {
validationException = addValidationError(
"API key metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]",
validationException
);
}
if (roleDescriptors != null) {
for (RoleDescriptor roleDescriptor : roleDescriptors) {
validationException = RoleDescriptorRequestValidator.validate(roleDescriptor, validationException);
}
}
return validationException;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(id);
writeOptionalList(out);
out.writeGenericMap(metadata);
}

private List<RoleDescriptor> readOptionalList(StreamInput in) throws IOException {
return in.readBoolean() ? in.readList(RoleDescriptor::new) : null;
}

private void writeOptionalList(StreamOutput out) throws IOException {
if (roleDescriptors == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeList(roleDescriptors);
}
}
n1v0lg marked this conversation as resolved.
Show resolved Hide resolved

public String getId() {
return id;
}

public Map<String, Object> getMetadata() {
return metadata;
}

public List<RoleDescriptor> getRoleDescriptors() {
return roleDescriptors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.core.security.action.apikey;

import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

public final class UpdateApiKeyResponse extends ActionResponse implements ToXContentObject, Writeable {
private final boolean updated;

public UpdateApiKeyResponse(boolean updated) {
this.updated = updated;
}

public UpdateApiKeyResponse(StreamInput in) throws IOException {
super(in);
this.updated = in.readBoolean();
}

public boolean isUpdated() {
return updated;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field("updated", updated).endObject();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(updated);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UpdateApiKeyResponse that = (UpdateApiKeyResponse) o;
return updated == that.updated;
}

@Override
public int hashCode() {
return Objects.hash(updated);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.core.security.action.apikey;

import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UpdateApiKeyRequestTests extends ESTestCase {

public void testNullValuesValid() {
final var request = new UpdateApiKeyRequest("id", null, null);
assertNull(request.validate());
}

public void testSerialization() throws IOException {
final boolean roleDescriptorsPresent = randomBoolean();
final List<RoleDescriptor> descriptorList;
if (roleDescriptorsPresent == false) {
descriptorList = null;
} else {
final int numDescriptors = randomIntBetween(0, 4);
descriptorList = new ArrayList<>();
for (int i = 0; i < numDescriptors; i++) {
descriptorList.add(new RoleDescriptor("role_" + i, new String[] { "all" }, null, null));
}
}

final var id = randomAlphaOfLength(10);
final var metadata = ApiKeyTests.randomMetadata();
final var request = new UpdateApiKeyRequest(id, descriptorList, metadata);

try (BytesStreamOutput out = new BytesStreamOutput()) {
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
final var serialized = new UpdateApiKeyRequest(in);
assertEquals(id, serialized.getId());
assertEquals(descriptorList, serialized.getRoleDescriptors());
assertEquals(metadata, request.getMetadata());
}
}
}
}
Loading