Skip to content

Commit

Permalink
Add Tests for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
maggarwal13 committed Jan 3, 2025
1 parent fe546c6 commit 2e73133
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public ApiPublicKeyLoader(
.build();
}

//TODO[MA] : check if this is right way
@Cacheable(cacheNames = "JWTpublicKeyDetails")
@Nullable
public List<RSAKey> loadPublicKey() throws ParseException {
if(StringUtils.isEmpty(restUrl)) {
LOGGER.warn("Skipping JWT verification as url is null");
Expand All @@ -92,9 +90,10 @@ protected List<RSAKey> getListOfKeys(final String publicKeys) throws ParseExcept
final Map<String, Object> content = JSONObjectUtils.parse(publicKeys);
final List<RSAKey> rsaKeys = Lists.newArrayList();

for (final Map<String, Object> key : (List<Map<String, Object>>) content.get("keys")) {
for (final Map<String, Object> key : JSONObjectUtils.getJSONObjectArray(content, "keys")) {
rsaKeys.add(RSAKey.parse(key));
}

return rsaKeys;
} catch ( Exception e ) {
LOGGER.error("Failed to parse public key", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import net.ripe.db.whois.api.rest.mapper.FormattedClientAttributeMapper;
import net.ripe.db.whois.api.rest.mapper.WhoisObjectMapper;
import net.ripe.db.whois.common.apiKey.ApiKeyUtils;
import net.ripe.db.whois.common.rpsl.AttributeType;
import net.ripe.db.whois.common.rpsl.RpslAttribute;
import net.ripe.db.whois.common.rpsl.RpslObject;
import net.ripe.db.whois.common.rpsl.RpslObjectBuilder;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -26,6 +29,7 @@

import java.time.LocalDateTime;

import static jakarta.ws.rs.client.Entity.entity;
import static jakarta.ws.rs.core.Response.Status.OK;
import static jakarta.ws.rs.core.Response.Status.UNAUTHORIZED;
import static net.ripe.db.whois.api.ApiKeyAuthServerDummy.BASIC_AUTH_INVALID_API_KEY;
Expand All @@ -35,6 +39,8 @@
import static net.ripe.db.whois.api.ApiKeyAuthServerDummy.BASIC_AUTH_TEST_NO_MNT;
import static net.ripe.db.whois.api.ApiKeyAuthServerDummy.BASIC_AUTH_TEST_TEST_MNT;
import static net.ripe.db.whois.api.rest.WhoisRestBasicAuthTestIntegration.getBasicAuthenticationHeader;
import static net.ripe.db.whois.common.rpsl.ObjectType.PERSON;
import static net.ripe.db.whois.common.rpsl.ObjectType.ROLE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
Expand All @@ -60,6 +66,7 @@ public class WhoisRestApiKeyAuthTestIntegration extends AbstractHttpsIntegration
"address: Singel 258\n" +
"phone: +31 6 12345678\n" +
"nic-hdl: TR2-TEST\n" +
"e-mail: [email protected]\n" +
"mnt-by: OWNER-MNT\n" +
"source: TEST";

Expand Down Expand Up @@ -439,6 +446,139 @@ public void create_object_with_apikey_differnt_mnt_fails() {
assertThat(response.getStatus(), is(UNAUTHORIZED.getStatusCode()));
}

@Test
public void update_object_with_apikey_no_mnt_with_sso() {
final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final WhoisResources whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_PERSON_NO_MNT))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), WhoisResources.class);

assertThat(whoisResources.getWhoisObjects().size(), is(1));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueForAttribute(AttributeType.REMARKS), is("more_test"));

}

@Test
public void update_object_with_apikey_with_mnt_with_sso() {
final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final WhoisResources whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_PERSON_OWNER_MNT))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), WhoisResources.class);

assertThat(whoisResources.getWhoisObjects().size(), is(1));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueForAttribute(AttributeType.REMARKS), is("more_test"));
}

@Test
public void update_object_with_invalid_apikey() {

final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final Response whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_INVALID_API_KEY))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), Response.class);

assertThat(whoisResources.getStatus(), is(UNAUTHORIZED.getStatusCode()));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueOrNullForAttribute(AttributeType.REMARKS), is(nullValue()));

}

@Test
public void update_object_with_valid_apikey_invalid_jwt_signature() {

final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final Response whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_INVALID_SIGNATURE_API_KEY))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), Response.class);

assertThat(whoisResources.getStatus(), is(UNAUTHORIZED.getStatusCode()));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueOrNullForAttribute(AttributeType.REMARKS), is(nullValue()));
}

@Test
public void update_object_with_apikey_different_mnt_fails() {

final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final Response whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_TEST_TEST_MNT))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), Response.class);

assertThat(whoisResources.getStatus(), is(UNAUTHORIZED.getStatusCode()));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueOrNullForAttribute(AttributeType.REMARKS), is(nullValue()));

}

@Test
public void update_object_with_apikey_different_mnt_same_sso_fails() {
databaseHelper.addObject(RpslObject.parse("" +
"mntner: TEST-MNT\n" +
"descr: Owner Maintainer\n" +
"admin-c: TP1-TEST\n" +
"upd-to: [email protected]\n" +
"auth: MD5-PW $1$d9fKeTr2$Si7YudNf4rUGmR71n/cqk/ #test\n" +
"auth: SSO [email protected]\n" +
"auth: SSO [email protected]\n" +
"mnt-by: OWNER-MNT\n" +
"source: TEST"));


final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final Response whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_TEST_TEST_MNT))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), Response.class);

assertThat(whoisResources.getStatus(), is(UNAUTHORIZED.getStatusCode()));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueOrNullForAttribute(AttributeType.REMARKS), is(nullValue()));
}

@Test
public void update_object_with_apikey_same_mnt_different_sso_fails() {
databaseHelper.updateObject(RpslObject.parse("" +
"mntner: OWNER-MNT\n" +
"descr: Owner Maintainer\n" +
"admin-c: TP1-TEST\n" +
"upd-to: [email protected]\n" +
"auth: MD5-PW $1$d9fKeTr2$Si7YudNf4rUGmR71n/cqk/ #test\n" +
"auth: SSO [email protected]\n" +
"mnt-by: OWNER-MNT\n" +
"source: TEST"));

final RpslObject updated = new RpslObjectBuilder(TEST_ROLE)
.addAttributeSorted(new RpslAttribute(AttributeType.REMARKS, "more_test"))
.get();

final Response whoisResources = SecureRestTest.target(getSecurePort(), "whois/TEST/role/TR2-TEST")
.request(MediaType.APPLICATION_XML)
.header(HttpHeaders.AUTHORIZATION, getBasicAuthHeader(BASIC_AUTH_TEST_TEST_MNT))
.put(Entity.entity(map(updated), MediaType.APPLICATION_XML), Response.class);

assertThat(whoisResources.getStatus(), is(UNAUTHORIZED.getStatusCode()));
assertThat(databaseHelper.lookupObject(ROLE, updated.getKey().toString()).getValueOrNullForAttribute(AttributeType.REMARKS), is(nullValue()));
}

private static void assertIrt(final WhoisObject whoisObject, final boolean isFIltered) {
assertThat(whoisObject.getAttributes(), contains(
new Attribute("irt", "irt-test"),
Expand Down

0 comments on commit 2e73133

Please sign in to comment.