Skip to content

Commit

Permalink
feat(add-api-v1) Add new resources handling qualified id requests fro…
Browse files Browse the repository at this point in the history
…m the backend

* Refactor resources and models in specific packages
* Add /api-version endpoint return version 0 and 1
* Add DeviceManagementService, shared service between resources, also abstracting database access from api layer
* Basic integration tests for the new api layer
  • Loading branch information
spoonman01 committed Oct 3, 2024
1 parent 7f59827 commit bae2b46
Show file tree
Hide file tree
Showing 38 changed files with 851 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// along with this program. If not, see http://www.gnu.org/licenses/.
//

package com.wire.bots.hold.model;
package com.wire.bots.hold;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wire/bots/hold/DAO/AccessDAO.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.hold.DAO;

import com.wire.bots.hold.model.LHAccess;
import com.wire.bots.hold.model.database.LHAccess;
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.hold.DAO;

import com.wire.bots.hold.model.LHAccess;
import com.wire.bots.hold.model.database.LHAccess;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wire/bots/hold/DAO/EventsDAO.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.hold.DAO;

import com.wire.bots.hold.model.Event;
import com.wire.bots.hold.model.database.Event;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wire.bots.hold.model.Event;
import com.wire.bots.hold.model.database.Event;
import com.wire.xenon.tools.Logger;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/wire/bots/hold/NotificationProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wire.bots.hold.DAO.AccessDAO;
import com.wire.bots.hold.model.Config;
import com.wire.bots.hold.model.LHAccess;
import com.wire.bots.hold.model.Notification;
import com.wire.bots.hold.model.NotificationList;
import com.wire.bots.hold.model.database.LHAccess;
import com.wire.helium.LoginClient;
import com.wire.helium.models.Access;
import com.wire.xenon.backend.models.Payload;
Expand Down
53 changes: 41 additions & 12 deletions src/main/java/com/wire/bots/hold/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.wire.bots.cryptobox.CryptoException;
import com.wire.bots.hold.DAO.AccessDAO;
import com.wire.bots.hold.DAO.EventsDAO;
import com.wire.bots.hold.filters.ServiceAuthenticationFilter;
import com.wire.bots.hold.healthchecks.SanityCheck;
import com.wire.bots.hold.model.Config;
import com.wire.bots.hold.monitoring.RequestMdcFactoryFilter;
import com.wire.bots.hold.monitoring.StatusResource;
import com.wire.bots.hold.resource.*;
import com.wire.bots.hold.resource.v0.audit.*;
import com.wire.bots.hold.resource.v0.backend.ConfirmResourceV0;
import com.wire.bots.hold.resource.v0.backend.InitiateResourceV0;
import com.wire.bots.hold.resource.v0.backend.RemoveResourceV0;
import com.wire.bots.hold.resource.v1.backend.ConfirmResourceV1;
import com.wire.bots.hold.resource.v1.backend.InitiateResourceV1;
import com.wire.bots.hold.resource.v1.backend.RemoveResourceV1;
import com.wire.bots.hold.service.DeviceManagementService;
import com.wire.bots.hold.utils.CryptoDatabaseFactory;
import com.wire.bots.hold.utils.HoldClientRepo;
import com.wire.bots.hold.utils.ImagesBundle;
import com.wire.helium.LoginClient;
import com.wire.xenon.Const;
import com.wire.xenon.backend.models.QualifiedId;
import com.wire.xenon.crypto.Crypto;
import com.wire.xenon.crypto.CryptoDatabase;
import com.wire.xenon.crypto.storage.JdbiStorage;
import com.wire.xenon.factories.CryptoFactory;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.client.JerseyClientBuilder;
Expand All @@ -54,6 +62,7 @@
import org.jdbi.v3.sqlobject.SqlObjectPlugin;

import javax.ws.rs.client.Client;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class Service extends Application<Config> {
Expand Down Expand Up @@ -105,19 +114,24 @@ public void run(Config config, Environment environment) {
final Client httpClient = createHttpClient(config, environment);
jdbi = buildJdbi(config.database, environment);

final CryptoFactory cf = getCryptoFactory(jdbi);
final CryptoDatabaseFactory cf = getCryptoFactory(jdbi);

final AccessDAO accessDAO = jdbi.onDemand(AccessDAO.class);
final EventsDAO eventsDAO = jdbi.onDemand(EventsDAO.class);

final DeviceManagementService deviceManagementService = new DeviceManagementService(accessDAO, cf);

// Monitoring resources
addResource(new StatusResource());
addResource(new RequestMdcFactoryFilter());

// Used by Wire Server
addResource(new InitiateResource(cf));
addResource(new ConfirmResource(accessDAO));
addResource(new RemoveResource(accessDAO, cf));
addResource(new InitiateResourceV0(deviceManagementService));
addResource(new InitiateResourceV1(deviceManagementService));
addResource(new ConfirmResourceV0(deviceManagementService));
addResource(new ConfirmResourceV1(deviceManagementService));
addResource(new RemoveResourceV0(deviceManagementService));
addResource(new RemoveResourceV1(deviceManagementService));

// Used by Audit
addResource(new AuthorizeResource());
Expand Down Expand Up @@ -185,10 +199,25 @@ protected void setupDatabase(Config.Database database) {
flyway.migrate();
}

public CryptoFactory getCryptoFactory(Jdbi jdbi) {
return (botId) -> new CryptoDatabase(
new QualifiedId(botId, null), // TODO(WPB-11287): Change null to default domain
new JdbiStorage(jdbi)
);
public CryptoDatabaseFactory getCryptoFactory(Jdbi jdbi) {
return new CryptoDatabaseFactory() {
@Override
public Crypto create(UUID botId) throws CryptoException {
// Note: the name botId is incorrect as in LegalHold we are creating crypto box based on users
// but in the Xenon library used by bots and LegalHold, this param was called botId
return new CryptoDatabase(
new QualifiedId(botId, null),
new JdbiStorage(jdbi)
);
}

@Override
public Crypto create(QualifiedId userId) throws CryptoException {
return new CryptoDatabase(
userId,
new JdbiStorage(jdbi)
);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.codahale.metrics.health.HealthCheck;
import com.wire.bots.hold.DAO.AccessDAO;
import com.wire.bots.hold.model.LHAccess;
import com.wire.bots.hold.model.database.LHAccess;
import com.wire.helium.API;
import com.wire.xenon.backend.models.QualifiedId;
import com.wire.xenon.tools.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wire.bots.hold.model.api.shared;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class ApiVersionResponse {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private final List<Integer> supported;

public ApiVersionResponse(List<Integer> supported) {
this.supported = supported;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wire.bots.hold.model;
package com.wire.bots.hold.model.api.shared;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wire.bots.hold.model;
package com.wire.bots.hold.model.api.v0;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -7,7 +7,7 @@
import java.util.UUID;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ConfirmPayload {
public class ConfirmPayloadV0 {
@JsonProperty("refresh_token")
@NotNull
public String refreshToken;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wire.bots.hold.model;
package com.wire.bots.hold.model.api.v0;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -7,7 +7,7 @@
import java.util.UUID;

@JsonIgnoreProperties(ignoreUnknown = true)
public class InitPayload {
public class InitPayloadV0 {
@JsonProperty("user_id")
@NotNull
public UUID userId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.wire.bots.hold.model.api.v1;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.wire.xenon.backend.models.QualifiedId;

import javax.validation.constraints.NotNull;
import java.util.UUID;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ConfirmPayloadV1 {
@JsonProperty("refresh_token")
@NotNull
public String refreshToken;

@JsonProperty("client_id")
@NotNull
public String clientId;

@JsonProperty("qualified_user_id")
@NotNull
public QualifiedId userId;

@JsonProperty("team_id")
@NotNull
public UUID teamId;
}
19 changes: 19 additions & 0 deletions src/main/java/com/wire/bots/hold/model/api/v1/InitPayloadV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wire.bots.hold.model.api.v1;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.wire.xenon.backend.models.QualifiedId;

import javax.validation.constraints.NotNull;
import java.util.UUID;

@JsonIgnoreProperties(ignoreUnknown = true)
public class InitPayloadV1 {
@JsonProperty("qualified_user_id")
@NotNull
public QualifiedId userId;

@JsonProperty("team_id")
@NotNull
public UUID teamId;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wire.bots.hold.model;
package com.wire.bots.hold.model.database;

import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wire.bots.hold.model;
package com.wire.bots.hold.model.database;

import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wire.bots.hold.model.dto;

import com.wire.xenon.models.otr.PreKey;

import java.util.ArrayList;

public class InitializedDeviceDTO {
private final ArrayList<PreKey> preKeys;
private final PreKey lastPreKey;
private final String fingerprint;

public InitializedDeviceDTO(ArrayList<PreKey> preKeys, PreKey lastPreKey, String fingerprint) {
this.preKeys = preKeys;
this.lastPreKey = lastPreKey;
this.fingerprint = fingerprint;
}

public ArrayList<PreKey> getPreKeys() {
return preKeys;
}

public PreKey getLastPreKey() {
return lastPreKey;
}

public String getFingerprint() {
return fingerprint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Wire
// Copyright (C) 2016 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//

package com.wire.bots.hold.monitoring;

import com.wire.bots.hold.model.api.shared.ApiVersionResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Api
@Path("/api-version")
@Produces(MediaType.APPLICATION_JSON)
public class ApiVersionResource {
@GET
@ApiOperation(value = "Api version")
public Response apiVersion() {
ApiVersionResponse response = new ApiVersionResponse(List.of(0,1));

return Response
.ok(response)
.build();
}
}
Loading

0 comments on commit bae2b46

Please sign in to comment.