Skip to content

Commit

Permalink
Merge pull request #699 from eclipse-passage/572366-2
Browse files Browse the repository at this point in the history
Bug 572366 verify product's public key on hc-fls interaction
  • Loading branch information
eparovyshnaya authored Mar 31, 2021
2 parents 0fd667e + f7b60df commit 3b26a52
Show file tree
Hide file tree
Showing 41 changed files with 544 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ protected CanUse(NetRequest request) {
}

@Override
protected Optional<NetResponse> invalid() {
protected Optional<NetResponse> rawInvalid() {
Optional<String> feature = feature();
if (!feature.isPresent()) {
return Optional.of(new Failure.BadRequestNoFeature());
}
return Optional.empty();
}

@Override
protected Optional<NetResponse> productUserInvalid(ProductUserRequest<NetRequest> refined) {
return Optional.empty();
}

private Optional<String> feature() {
return new FeatureIdentifier(key -> data.parameter(key)).get();
}
Expand Down
40 changes: 0 additions & 40 deletions bundles/org.eclipse.passage.lbc.base/.settings/.api_filters

This file was deleted.

8 changes: 1 addition & 7 deletions bundles/org.eclipse.passage.lbc.base/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: javax.servlet;bundle-version="0.0.0",
org.eclipse.osgi;bundle-version="0.0.0",
Require-Bundle: org.eclipse.osgi;bundle-version="0.0.0",
org.eclipse.passage.lic.base;bundle-version="0.0.0",
org.eclipse.passage.lic.net;bundle-version="0.0.0",
org.eclipse.passage.lic.licenses.model;bundle-version="0.0.0",
org.eclipse.emf.ecore.xmi;bundle-version="0.0.0",
org.eclipse.passage.lic.json;bundle-version="0.0.0",
com.fasterxml.jackson.core.jackson-databind;bundle-version="0.0.0",
com.fasterxml.jackson.core.jackson-core;bundle-version="0.0.0",
org.eclipse.passage.lic.floating;bundle-version="0.1.0",
org.eclipse.passage.lic.bc;bundle-version="1.0.100",
org.eclipse.passage.lic.floating.model;bundle-version="0.1.0",
org.eclipse.passage.lic.licenses.migration;bundle-version="0.5.300",
org.eclipse.passage.lic.emf;bundle-version="1.0.100",
org.eclipse.passage.lic.oshi;bundle-version="1.0.1",
org.apache.logging.log4j;bundle-version="2.8.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
*******************************************************************************/
package org.eclipse.passage.lbc.internal.base;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.internal.api.EvaluationInstructions;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct;
import org.eclipse.passage.lic.internal.base.io.PassageFileExtension;
import org.eclipse.passage.lic.internal.base.io.PathFromLicensedProduct;
import org.eclipse.passage.lic.internal.net.api.handle.NetResponse;
import org.eclipse.passage.lic.internal.net.handle.ChoreDraft;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.ProductUserRequest;

abstract class AuthentifiedChoreDraft extends ChoreDraft<RawRequest> {

Expand All @@ -27,18 +34,38 @@ protected AuthentifiedChoreDraft(RawRequest request) {
}

@Override
protected Optional<NetResponse> invalid() {
protected Optional<NetResponse> rawInvalid() {
return serverIsNotAuthenticated();
}

@Override
protected Optional<NetResponse> productUserInvalid(ProductUserRequest<RawRequest> refined) {
return productUnknown(refined);
}

private Optional<NetResponse> serverIsNotAuthenticated() {
Optional<EvaluationInstructions> instructions = new ServerAuthenticationInstructions(data).get();
if (!instructions.isPresent()) {
return Optional.of(new Failure.BadRequestInvalidServerAuthInstructions());
}
try {
new ServerAuthentication(instructions.get()).evaluate();
} catch (Exception e) {
log.error("failed: ", e); //$NON-NLS-1$
log.error("Failed on server authentication attempt: ", e); //$NON-NLS-1$
return Optional.of(new Failure.ForeignServer(e.getMessage()));
}
return Optional.empty();
}

private Optional<NetResponse> productUnknown(ProductUserRequest<RawRequest> refined) {
LicensedProduct product = refined.product().get();
Path key = new PathFromLicensedProduct(data.state()::source, product).get()//
.resolve(new FileNameFromLicensedProduct(product, new PassageFileExtension.PublicKey()).get());
if (!Files.exists(key)) {
log.error(String.format("Key file [%s] is not found", product)); //$NON-NLS-1$
return Optional.of(new Failure.BadRequestUnkonwnProduct(product));
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@
public final class EagerFloatingState implements FloatingState {

private final Grants grants;
private final Path source;

public EagerFloatingState() {
this(new LicensingFolder(new UserHomePath()));
}

public EagerFloatingState(Supplier<Path> source) {
this.grants = new AcquiredGrants(source);
this(new AcquiredGrants(source), source.get());
}

public EagerFloatingState(Grants grants, Path source) {
this.grants = grants;
this.source = source;
}

@Override
public Grants grants() {
return grants;
}

@Override
public Path source() {
return source;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lbc.internal.base;

import java.util.Optional;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.passage.lbc.internal.base.api.FlsGear;
import org.eclipse.passage.lbc.internal.base.api.FlsGearAwre;
import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.internal.api.io.Hashes;
import org.eclipse.passage.lic.internal.api.io.KeyKeeper;
import org.eclipse.passage.lic.internal.api.registry.StringServiceId;
import org.eclipse.passage.lic.internal.net.api.handle.NetResponse;
import org.eclipse.passage.lic.internal.net.handle.EObjectTransfer;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.ProductUserRequest;

public final class EncodedResponse<T extends EObject> {

private final T payload;
private final ProductUserRequest<RawRequest> data;

public EncodedResponse(T payload, ProductUserRequest<RawRequest> data) {
this.payload = payload;
this.data = data;
}

public NetResponse get() {
return new FlsGearAwre()//
.withGear(this::transferable) //
.orElse(new Failure.OperationFailed("mine", "Failed exploiting gear")); //$NON-NLS-1$ //$NON-NLS-2$
}

private Optional<NetResponse> transferable(FlsGear gear) {
return Optional.of(new EObjectTransfer(payload, keyKeeper(gear), hashes(gear)));
}

private KeyKeeper keyKeeper(FlsGear gear) {
return gear.keyKeper(data.product().get(), data.raw().state()::source);
}

private Hashes hashes(FlsGear gear) {
return gear.hashes().get().service(new StringServiceId(data.algorithm().get()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.passage.lbc.internal.base.EncodedResponse;
import org.eclipse.passage.lbc.internal.base.api.Grants;
import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.floating.model.api.GrantAcqisition;
Expand All @@ -28,7 +30,6 @@
import org.eclipse.passage.lic.internal.base.FeatureIdentifier;
import org.eclipse.passage.lic.internal.emf.EObjectFromBytes;
import org.eclipse.passage.lic.internal.net.api.handle.NetResponse;
import org.eclipse.passage.lic.internal.net.handle.EObjectTransfer;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.PlainSuceess;
import org.eclipse.passage.lic.internal.net.handle.ProductUserRequest;
Expand Down Expand Up @@ -58,7 +59,11 @@ public NetResponse get() {
if (!acquisition.isPresent()) {
return noGrants(feature.get());
}
return new EObjectTransfer(acquisition.get());
return encodedPack(acquisition.get());
}

private NetResponse encodedPack(GrantAcqisition acqisition) {
return new EncodedResponse<EObject>(acqisition, data).get();
}

public NetResponse returnBack() throws LicensingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.passage.lbc.internal.base.api.FlsGear;
import org.eclipse.passage.lbc.internal.base.api.FlsGearAwre;
import org.eclipse.passage.lic.floating.model.api.FeatureGrant;
import org.eclipse.passage.lic.floating.model.api.FloatingLicensePack;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
Expand All @@ -29,6 +34,7 @@ final class FeatureGrants {
private final String user;
private final String feature;
private final Supplier<Path> base;
private final Logger log = LogManager.getLogger(getClass());

FeatureGrants(LicensedProduct product, String user, String feature, Supplier<Path> base) {
this.product = product;
Expand All @@ -41,13 +47,34 @@ final class FeatureGrants {
* Explore all licenses for the {@code product} and collect all grants for the
* given {@code feature}, if any
*/
Collection<FeatureGrant> get() throws LicensingException {
return new LicensePacks(product, base).get().stream()//
.filter(new AvailableForUser(user)) //
.map(this::grantForFeature) //
.filter(Optional::isPresent) //
.map(Optional::get) //
.collect(Collectors.toList());
Collection<FeatureGrant> get() {
return new FlsGearAwre()//
.withGear(gear -> Optional.of(get(gear)))//
.orElse(failedOnGathering());
}

private Collection<FeatureGrant> failedOnGathering() {
log.error(String.format("Failed on gathering grans for product %s", product)); //$NON-NLS-1$
return Collections.emptyList();
}

private Collection<FeatureGrant> get(FlsGear gear) {
try {
return new LicensePacks(//
gear.keyKeper(product, base), //
gear.codec(product), //
product, //
base//
).get().stream()//
.filter(new AvailableForUser(user)) //
.map(this::grantForFeature) //
.filter(Optional::isPresent) //
.map(Optional::get) //
.collect(Collectors.toList());
} catch (LicensingException e) {
log.error(e);
return Collections.emptyList();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import org.eclipse.passage.lic.internal.base.conditions.mining.DecodedContent;
import org.eclipse.passage.lic.internal.base.io.FileCollection;
import org.eclipse.passage.lic.internal.base.io.PathFromLicensedProduct;
import org.eclipse.passage.lic.internal.base.io.PathKeyKeeper;
import org.eclipse.passage.lic.internal.bc.BcStreamCodec;
import org.eclipse.passage.lic.internal.emf.EObjectFromBytes;

final class LicensePacks {
Expand All @@ -43,10 +41,10 @@ final class LicensePacks {
private final Supplier<Path> base;
private final Logger log = LogManager.getLogger(getClass());

LicensePacks(LicensedProduct product, Supplier<Path> base) {
public LicensePacks(KeyKeeper key, StreamCodec codec, LicensedProduct product, Supplier<Path> base) {
this.product = product;
this.key = new PathKeyKeeper(product, base);
this.codec = new BcStreamCodec(() -> product);
this.key = key;
this.codec = codec;
this.base = base;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
* Copyright (c) 2020, 2021 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.passage.lbc.internal.base.api;

import java.nio.file.Path;

/**
* FIXME: temporary
*
Expand All @@ -23,4 +25,6 @@ public interface FloatingState {

Grants grants();

Path source();

}
Loading

0 comments on commit 3b26a52

Please sign in to comment.