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

[HAL-1992] Support signature validation #1232

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
@@ -0,0 +1,177 @@
/*
* Copyright 2022 Red Hat
*
* 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
*
* https://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 org.jboss.hal.client.installer;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.jboss.hal.ballroom.dialog.DialogFactory;
import org.jboss.hal.core.finder.ColumnAction;
import org.jboss.hal.core.finder.ColumnActionFactory;
import org.jboss.hal.core.finder.Finder;
import org.jboss.hal.core.finder.FinderColumn;
import org.jboss.hal.core.finder.ItemAction;
import org.jboss.hal.core.finder.ItemDisplay;
import org.jboss.hal.dmr.Operation;
import org.jboss.hal.dmr.ResourceAddress;
import org.jboss.hal.dmr.dispatch.Dispatcher;
import org.jboss.hal.meta.StatementContext;
import org.jboss.hal.meta.security.Constraint;
import org.jboss.hal.resources.CSS;
import org.jboss.hal.resources.Icons;
import org.jboss.hal.resources.Ids;
import org.jboss.hal.resources.Names;
import org.jboss.hal.resources.Resources;
import org.jboss.hal.resources.UIConstants;
import org.jboss.hal.spi.Callback;
import org.jboss.hal.spi.Column;
import org.jboss.hal.spi.Message;
import org.jboss.hal.spi.MessageEvent;
import org.jboss.hal.spi.Requires;

import com.google.web.bindery.event.shared.EventBus;

import elemental2.dom.HTMLElement;
import elemental2.promise.Promise;

import static java.util.stream.Collectors.toList;

import static org.jboss.hal.client.installer.AddressTemplates.INSTALLER_ADDRESS;
import static org.jboss.hal.client.installer.AddressTemplates.INSTALLER_TEMPLATE;
import static org.jboss.hal.core.finder.FinderColumn.RefreshMode.CLEAR_SELECTION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CERTIFICATES;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CERTIFICATE_REMOVE;
import static org.jboss.hal.dmr.ModelDescriptionConstants.INCLUDE_RUNTIME;
import static org.jboss.hal.dmr.ModelDescriptionConstants.KEY_ID;
import static org.jboss.hal.dmr.ModelDescriptionConstants.READ_RESOURCE_OPERATION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.WRITE_ATTRIBUTE_OPERATION;

@Column(Ids.UPDATE_MANAGER_CERTIFICATE)
@Requires(INSTALLER_ADDRESS)
public class CertificateColumn extends FinderColumn<CertificateInfo> {

private final Resources resources;
private final EventBus eventBus;
private final Dispatcher dispatcher;
private final StatementContext statementContext;

@Inject
public CertificateColumn(final Finder finder,
final EventBus eventBus,
final ColumnActionFactory columnActionFactory,
final StatementContext statementContext,
final Dispatcher dispatcher,
final Resources resources) {
super(new Builder<CertificateInfo>(finder, Ids.UPDATE_MANAGER_CERTIFICATE, Names.CERTIFICATES)
.onPreview((CertificateInfo c) -> new CertificatePreview(c))
.showCount()
.withFilter());

this.eventBus = eventBus;
this.dispatcher = dispatcher;
this.statementContext = statementContext;
this.resources = resources;

setItemsProvider(context -> {
ResourceAddress address = INSTALLER_TEMPLATE.resolve(statementContext);
Operation operation = new Operation.Builder(address, READ_RESOURCE_OPERATION)
.param(INCLUDE_RUNTIME, true)
.build();
return dispatcher.execute(operation).then(result -> {
// noinspection Convert2MethodRef
List<CertificateInfo> certificates = result.get(CERTIFICATES).asList().stream()
.map(node -> new CertificateInfo(node))
.collect(toList());
return Promise.resolve(certificates);
});
});

setItemRenderer(item -> new ItemDisplay<CertificateInfo>() {
@Override
public String getId() {
return Ids.asId(item.getKeyID());
}

@Override
public String getTitle() {
return item.getDescription();
}

@Override
public HTMLElement getIcon() {
if (item.getStatus().equals(CertificateInfo.TRUSTED)) {
return Icons.ok();
} else {
return Icons.warning();
}
}

@Override
public HTMLElement element() {
return ItemDisplay.withSubtitle(item.getDescription(), item.getKeyID());
}

@Override
public List<ItemAction<CertificateInfo>> actions() {
List<ItemAction<CertificateInfo>> actions = new ArrayList<>();
actions.add(new ItemAction.Builder<CertificateInfo>()
.title(resources.constants().remove())
.handler(itm -> remove(itm))
.constraint(Constraint.executable(INSTALLER_TEMPLATE, WRITE_ATTRIBUTE_OPERATION))
.build());
return actions;
}
});

addColumnAction(new ColumnAction.Builder<CertificateInfo>(Ids.UPDATE_MANAGER_CERTIFICATE_ADD)
.element(columnActionFactory.addButton(resources.constants().importCertificate(),
CSS.pfIcon(UIConstants.ADD_CIRCLE_O)))
.handler(column -> add())
.constraint(Constraint.executable(INSTALLER_TEMPLATE, WRITE_ATTRIBUTE_OPERATION))
.build());
addColumnAction(columnActionFactory.refresh(Ids.UPDATE_MANAGER_CERTIFICATE_REFRESH));
}

private void add() {
new ImportComponentCertificateWizard(dispatcher, statementContext, resources).show(this);
}

private void remove(CertificateInfo certificate) {
DialogFactory.showConfirmation(resources.constants().removeComponentCertificate(),
resources.messages().removeUpdateCertificateQuestion(certificate.getKeyID()),
() -> removeCertificate(certificate.getKeyID(), () -> refresh(CLEAR_SELECTION)));
}

void removeCertificate(final String name, final Callback callback) {
Operation operation = new Operation.Builder(INSTALLER_TEMPLATE.resolve(statementContext), CERTIFICATE_REMOVE)
.param(KEY_ID, name)
.build();
dispatcher.execute(operation)
.then(result -> {
MessageEvent.fire(eventBus, Message.success(resources.messages().removeResourceSuccess(Names.CERTIFICATE,
name)));
return null;
})
.catch_(failure -> {
MessageEvent.fire(eventBus,
Message.error(resources.messages().lastOperationFailed(), String.valueOf(failure)));
return null;
})
.finally_(callback::execute);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022 Red Hat
*
* 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
*
* https://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 org.jboss.hal.client.installer;

import org.jboss.hal.dmr.ModelNode;
import org.jboss.hal.dmr.NamedNode;

public class CertificateInfo extends NamedNode {

protected static final String TRUSTED = "TRUSTED";

CertificateInfo(final ModelNode node) {
super(node.get("key-id").asString(), node);
}

String getKeyID() {
return get("key-id").asString();
}

public String getFingerprint() {
return get("fingerprint").asString();
}

public String getDescription() {
return get("description").asString();
}

public String getStatus() {
return get("status").asString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022 Red Hat
*
* 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
*
* https://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 org.jboss.hal.client.installer;

import org.jboss.hal.ballroom.LabelBuilder;
import org.jboss.hal.core.finder.PreviewAttributes;
import org.jboss.hal.core.finder.PreviewContent;
import org.jboss.hal.dmr.ModelDescriptionConstants;

import static org.jboss.hal.dmr.ModelDescriptionConstants.DESCRIPTION;
import static org.jboss.hal.dmr.ModelDescriptionConstants.KEY_ID;
import static org.jboss.hal.dmr.ModelDescriptionConstants.STATUS;

class CertificatePreview extends PreviewContent<CertificateInfo> {

CertificatePreview(CertificateInfo certificate) {
super(certificate.getKeyID());

LabelBuilder labelBuilder = new LabelBuilder();
PreviewAttributes<CertificateInfo> attributes = new PreviewAttributes<>(certificate);
attributes
.append(model -> new PreviewAttributes.PreviewAttribute(labelBuilder.label(KEY_ID), certificate.getKeyID()));
attributes.append(
model -> new PreviewAttributes.PreviewAttribute(labelBuilder.label(ModelDescriptionConstants.FINGERPRINT),
certificate.getFingerprint()));
attributes.append(
model -> new PreviewAttributes.PreviewAttribute(labelBuilder.label(DESCRIPTION), certificate.getDescription()));
attributes
.append(model -> new PreviewAttributes.PreviewAttribute(labelBuilder.label(STATUS), certificate.getStatus()));

previewBuilder().addAll(attributes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2022 Red Hat
*
* 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
*
* https://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 org.jboss.hal.client.installer;

import java.util.Collections;
import java.util.List;

import org.jboss.hal.ballroom.wizard.AsyncStep;
import org.jboss.hal.ballroom.wizard.WizardStep;
import org.jboss.hal.ballroom.wizard.WorkflowCallback;
import org.jboss.hal.core.mbui.form.ModelNodeForm;
import org.jboss.hal.dmr.ModelNode;
import org.jboss.hal.dmr.Operation;
import org.jboss.hal.dmr.dispatch.Dispatcher;
import org.jboss.hal.flow.FlowContext;
import org.jboss.hal.flow.Progress;
import org.jboss.hal.flow.Task;
import org.jboss.hal.meta.Metadata;
import org.jboss.hal.meta.StatementContext;
import org.jboss.hal.resources.Ids;
import org.jboss.hal.resources.Resources;

import elemental2.dom.HTMLElement;
import elemental2.promise.Promise;

import static org.jboss.elemento.Elements.div;
import static org.jboss.hal.client.installer.AddressTemplates.INSTALLER_TEMPLATE;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CERTIFICATE_IMPORT;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CERT_FILE;
import static org.jboss.hal.flow.Flow.sequential;
import static org.jboss.hal.resources.CSS.marginBottomLarge;

class ConfirmComponentCertificateStep
extends WizardStep<ImportComponentCertificateContext, ImportComponentCertificateState>
implements AsyncStep<ImportComponentCertificateContext> {

private final Dispatcher dispatcher;
private final StatementContext statementContext;
private final Resources resources;

public ConfirmComponentCertificateStep(final Dispatcher dispatcher,
final StatementContext statementContext,
final Resources resources) {
super(resources.constants().confirmation());

this.dispatcher = dispatcher;
this.statementContext = statementContext;
this.resources = resources;

certificateForm = new ModelNodeForm.Builder<CertificateInfo>(Ids.build(Ids.UPDATE_MANAGER_LIST_UPDATES),
Metadata.staticDescription(UpdateManagerResources.INSTANCE.componentCertificate()))
.readOnly()
.build();
registerAttachable(certificateForm);

root = div()
.add(div().css(marginBottomLarge).innerHtml(resources.messages().importComponentCertificateConfirmation()))
.add(certificateForm)
.element();
}

private final HTMLElement root;
private final ModelNodeForm<CertificateInfo> certificateForm;

@Override
public HTMLElement element() {
return root;
}

@Override
protected void onShow(final ImportComponentCertificateContext context) {
certificateForm.view(context.getImportedCertificate());
}

@Override
public void onNext(final ImportComponentCertificateContext context, final WorkflowCallback callback) {
Operation operation = new Operation.Builder(INSTALLER_TEMPLATE.resolve(statementContext), CERTIFICATE_IMPORT)
.param(CERT_FILE, new ModelNode().set(0))
.build();
List<Task<FlowContext>> tasks = Collections.singletonList(
flowContext -> dispatcher.upload(context.getFile(), operation).then(result -> {
context.setImportedCertificate(new CertificateInfo(result.get()));
return Promise.resolve(flowContext);
}));

sequential(new FlowContext(Progress.NOOP), tasks)
.timeout(Timeouts.UPLOAD * 1_000)
.then(flowContext -> {
callback.proceed();
return Promise.resolve(context);
})
.catch_(failure -> {
if (FlowContext.timeout(failure)) {
wizard().showError(resources.constants().timeout(), resources.messages().operationTimeout(), false);
} else {
wizard().showError(resources.constants().error(),
resources.messages().uploadError(context.getFile().name),
String.valueOf(failure), false);
}
return Promise.reject(failure);
});
}
}
Loading