Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ public Uni<List<ComponentModel>> getComponents(NamespaceEntity namespace, String
);
}

public Uni<ComponentModel> getComponent(String id) {
return ComponentEntity.<ComponentEntity>findById(id)
public Uni<ComponentModel> getComponent(NamespaceEntity namespace, String id) {
return ComponentEntity
.<ComponentEntity>find("SELECT DISTINCT c FROM ComponentEntity c LEFT JOIN FETCH c.componentConfigs WHERE c.namespace.id = :namespaceId and c.id = :id",
Parameters.with("namespaceId", namespace.id).and("id", id)
)
.singleResult()
.map(entity -> entity != null ? entityToModel(entity) : null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
import io.github.project.openubl.ublhub.idm.*;
import io.github.project.openubl.ublhub.keys.component.ComponentModel;
import io.github.project.openubl.ublhub.keys.component.utils.ComponentUtil;
import io.github.project.openubl.ublhub.keys.provider.ProviderConfigProperty;
import io.github.project.openubl.ublhub.keys.utils.StripSecretsUtils;
import io.github.project.openubl.ublhub.models.jpa.entities.CompanyEntity;
import io.github.project.openubl.ublhub.models.jpa.entities.NamespaceEntity;
import io.github.project.openubl.ublhub.models.jpa.entities.SunatEntity;
import io.github.project.openubl.ublhub.models.jpa.entities.UBLDocumentEntity;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.ConfigPropertyRepresentation;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -146,27 +149,27 @@ public static ComponentRepresentation toRepresentationWithoutConfig(ComponentMod
return rep;
}

// public static List<ConfigPropertyRepresentation> toRepresentation(List<ProviderConfigProperty> configProperties) {
// List<ConfigPropertyRepresentation> propertiesRep = new LinkedList<>();
// for (ProviderConfigProperty prop : configProperties) {
// ConfigPropertyRepresentation propRep = toRepresentation(prop);
// propertiesRep.add(propRep);
// }
// return propertiesRep;
// }
//
// public static ConfigPropertyRepresentation toRepresentation(ProviderConfigProperty prop) {
// ConfigPropertyRepresentation propRep = new ConfigPropertyRepresentation();
// propRep.setName(prop.getName());
// propRep.setLabel(prop.getLabel());
// propRep.setType(prop.getType());
// propRep.setDefaultValue(prop.getDefaultValue());
// propRep.setOptions(prop.getOptions());
// propRep.setHelpText(prop.getHelpText());
// propRep.setSecret(prop.isSecret());
// return propRep;
// }
//
public static List<ConfigPropertyRepresentation> toRepresentation(List<ProviderConfigProperty> configProperties) {
List<ConfigPropertyRepresentation> propertiesRep = new LinkedList<>();
for (ProviderConfigProperty prop : configProperties) {
ConfigPropertyRepresentation propRep = toRepresentation(prop);
propertiesRep.add(propRep);
}
return propertiesRep;
}

public static ConfigPropertyRepresentation toRepresentation(ProviderConfigProperty prop) {
ConfigPropertyRepresentation propRep = new ConfigPropertyRepresentation();
propRep.setName(prop.getName());
propRep.setLabel(prop.getLabel());
propRep.setType(prop.getType());
propRep.setDefaultValue(prop.getDefaultValue());
propRep.setOptions(prop.getOptions());
propRep.setHelpText(prop.getHelpText());
propRep.setSecret(prop.isSecret());
return propRep;
}

public static <T, R> PageRepresentation<R> toRepresentation(List<T> pageElements, Long totalElements, Function<T, R> mapper) {
PageRepresentation<R> rep = new PageRepresentation<>();

Expand All @@ -185,7 +188,4 @@ public static <T, R> PageRepresentation<R> toRepresentation(List<T> pageElements
return rep;
}

// private static URIBuilder getURIBuilder(UriInfo uriInfo) throws URISyntaxException {
// return new URIBuilder(uriInfo.getPath());
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.github.project.openubl.ublhub.models.utils;

import io.github.project.openubl.ublhub.keys.component.ComponentModel;
import io.github.project.openubl.ublhub.keys.component.utils.ComponentUtil;
import io.github.project.openubl.ublhub.keys.provider.ProviderConfigProperty;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.representations.idm.ComponentRepresentation;

import java.util.*;

public class RepresentationToModel {

public static ComponentModel toModel(ComponentRepresentation rep) {
ComponentModel model = new ComponentModel();
model.setId(rep.getId());
model.setParentId(rep.getParentId());
model.setProviderType(rep.getProviderType());
model.setProviderId(rep.getProviderId());
model.setConfig(new MultivaluedHashMap<>());
model.setName(rep.getName());
model.setSubType(rep.getSubType());

if (rep.getConfig() != null) {
Set<String> keys = new HashSet<>(rep.getConfig().keySet());
for (String k : keys) {
List<String> values = rep.getConfig().get(k);
if (values != null) {
ListIterator<String> itr = values.listIterator();
while (itr.hasNext()) {
String v = itr.next();
if (v == null || v.trim().isEmpty()) {
itr.remove();
}
}

if (!values.isEmpty()) {
model.getConfig().put(k, values);
}
}
}
}

return model;
}

public static void updateComponent(ComponentRepresentation rep, ComponentModel component, boolean internal, ComponentUtil componentUtil) {
if (rep.getName() != null) {
component.setName(rep.getName());
}

if (rep.getParentId() != null) {
component.setParentId(rep.getParentId());
}

if (rep.getProviderType() != null) {
component.setProviderType(rep.getProviderType());
}

if (rep.getProviderId() != null) {
component.setProviderId(rep.getProviderId());
}

if (rep.getSubType() != null) {
component.setSubType(rep.getSubType());
}

Map<String, ProviderConfigProperty> providerConfiguration = null;
if (!internal) {
providerConfiguration = componentUtil.getComponentConfigProperties(component);
}

if (rep.getConfig() != null) {
Set<String> keys = new HashSet<>(rep.getConfig().keySet());
for (String k : keys) {
if (!internal && !providerConfiguration.containsKey(k)) {
break;
}

List<String> values = rep.getConfig().get(k);
if (values == null || values.isEmpty() || values.get(0) == null || values.get(0).trim().isEmpty()) {
component.getConfig().remove(k);
} else {
ListIterator<String> itr = values.listIterator();
while (itr.hasNext()) {
String v = itr.next();
if (v == null || v.trim().isEmpty() || v.equals(ComponentRepresentation.SECRET_VALUE)) {
itr.remove();
}
}

if (!values.isEmpty()) {
component.getConfig().put(k, values);
}
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.github.project.openubl.ublhub.idm.PageRepresentation;
import io.github.project.openubl.ublhub.keys.DefaultKeyProviders;
import io.github.project.openubl.ublhub.keys.KeyManager;
import io.github.project.openubl.ublhub.keys.component.ComponentModel;
import io.github.project.openubl.ublhub.keys.component.utils.ComponentUtil;
import io.github.project.openubl.ublhub.models.PageBean;
import io.github.project.openubl.ublhub.models.SortBean;
Expand All @@ -30,6 +31,7 @@
import io.github.project.openubl.ublhub.models.jpa.entities.SunatEntity;
import io.github.project.openubl.ublhub.models.utils.EntityToRepresentation;
import io.github.project.openubl.ublhub.models.utils.RepresentationToEntity;
import io.github.project.openubl.ublhub.models.utils.RepresentationToModel;
import io.github.project.openubl.ublhub.resources.utils.ResourceUtils;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
Expand All @@ -38,6 +40,7 @@
import org.jboss.logging.Logger;
import org.keycloak.common.util.PemUtils;
import org.keycloak.crypto.KeyWrapper;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.KeysMetadataRepresentation;

import javax.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -233,6 +236,75 @@ public Uni<Response> getComponents(
.onItem().ifNull().continueWith(Response.status(Response.Status.NOT_FOUND)::build);
}

@POST
@Path("/{namespaceId}/components")
public Uni<Response> createComponent(
@PathParam("namespaceId") @NotNull String namespaceId,
ComponentRepresentation rep
) {
return Panache
.withTransaction(() -> namespaceRepository.findById(namespaceId)
.onItem().ifNotNull().transformToUni(namespace -> {
ComponentModel model = RepresentationToModel.toModel(rep);
model.setId(null);
return componentRepository.addComponentModel(namespace, model);
})
.map(entity -> Response.status(Response.Status.CREATED).build())
)
.onItem().ifNull().continueWith(Response.status(Response.Status.NOT_FOUND)::build);
}

@GET
@Path("/{namespaceId}/components/{componentId}")
public Uni<Response> getComponent(
@PathParam("namespaceId") @NotNull String namespaceId,
@PathParam("componentId") String componentId
) {
return Panache
.withTransaction(() -> namespaceRepository.findById(namespaceId)
.onItem().ifNotNull().transformToUni(namespace -> componentRepository.getComponent(namespace, componentId))
)
.onItem().ifNotNull().transform(componentModel -> Response.ok().entity(EntityToRepresentation.toRepresentation(componentModel, false, componentUtil)).build())
.onItem().ifNull().continueWith(Response.status(Response.Status.NOT_FOUND)::build);
}

@PUT
@Path("/{namespaceId}/components/{componentId}")
public Uni<Response> updateComponent(
@PathParam("namespaceId") @NotNull String namespaceId,
@PathParam("componentId") String componentId,
ComponentRepresentation rep
) {
return Panache
.withTransaction(() -> namespaceRepository.findById(namespaceId)
.onItem().ifNotNull().transformToUni(namespace -> componentRepository.getComponent(namespace, componentId)
.chain(componentModel -> {
RepresentationToModel.updateComponent(rep, componentModel, false, componentUtil);
return componentRepository
.updateComponent(namespace, componentModel)
.chain(() -> componentRepository.getComponent(namespace, componentId));
})
)
.map(componentModel -> Response.ok().entity(EntityToRepresentation.toRepresentation(componentModel, false, componentUtil)).build())
)
.onItem().ifNull().continueWith(Response.status(Response.Status.NOT_FOUND)::build);
}

@DELETE
@Path("/{namespaceId}/components/{componentId}")
public Uni<Response> removeComponent(
@PathParam("namespaceId") @NotNull String namespaceId,
@PathParam("componentId") String componentId
) {
return Panache
.withTransaction(() -> namespaceRepository.findById(namespaceId)
.onItem().ifNotNull().transformToUni(namespace -> componentRepository.getComponent(namespace, componentId)
.chain(componentModel -> componentRepository.removeComponent(componentModel))
)
.map((result) -> Response.ok().build())
)
.onItem().ifNull().continueWith(Response.status(Response.Status.NOT_FOUND)::build);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.github.project.openubl.ublhub.resources;

/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 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
*
* https://www.eclipse.org/legal/epl-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.
*/

import io.github.project.openubl.ublhub.idm.ServerInfoRepresentation;
import io.github.project.openubl.ublhub.keys.KeyProvider;
import io.github.project.openubl.ublhub.keys.KeyProviderFactory;
import io.github.project.openubl.ublhub.keys.component.ComponentFactory;
import io.github.project.openubl.ublhub.keys.provider.ProviderConfigProperty;
import io.github.project.openubl.ublhub.models.utils.EntityToRepresentation;
import org.keycloak.representations.idm.ComponentTypeRepresentation;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Path("/server-info")
@Produces("application/json")
@Consumes("application/json")
@Transactional
@ApplicationScoped
public class ServerInfoResource {

@Inject
@Any
Instance<KeyProviderFactory> componentFactories;

@GET
@Produces(MediaType.APPLICATION_JSON)
public ServerInfoRepresentation getInfo() {
ServerInfoRepresentation info = new ServerInfoRepresentation();
setProviders(info);
return info;
}

private void setProviders(ServerInfoRepresentation info) {
info.setComponentTypes(new HashMap<>());

List<ComponentTypeRepresentation> types = new ArrayList<>();

for (ComponentFactory componentFactory : componentFactories) {
ComponentTypeRepresentation rep = new ComponentTypeRepresentation();
rep.setId(componentFactory.getId());
rep.setHelpText(componentFactory.getHelpText());
List<ProviderConfigProperty> configProperties = componentFactory.getConfigProperties();
if (configProperties == null) {
configProperties = Collections.emptyList();
}
rep.setProperties(EntityToRepresentation.toRepresentation(configProperties));

types.add(rep);
}

info.getComponentTypes().put(KeyProvider.class.getName(), types);
info.getComponentTypes().put("keyProviders", types);
}

}

Loading