Skip to content

Commit

Permalink
Require acknowledgement to start_trial license
Browse files Browse the repository at this point in the history
This is related to elastic#30134. It modifies the start_trial action to require
an acknowledgement parameter in the rest request to actually start the
trial license. There are backwards compatibility issues as prior ES
versions did not support this parameter. To handle this, it is assumed
that a request coming from a node prior to 6.3 is acknowledged. And
attempts to write a non-acknowledged request to a prior to 6.3 node will
throw an exception.

Additionally this PR adds messages about the trial license the user is
generating.
  • Loading branch information
Tim-Brooks committed Apr 25, 2018
1 parent 0a5a9a2 commit a7629d9
Show file tree
Hide file tree
Showing 21 changed files with 1,396 additions and 39 deletions.
1 change: 0 additions & 1 deletion x-pack/docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ buildRestTests.expectedUnconvertedCandidates = [
'en/watcher/trigger/schedule/yearly.asciidoc',
'en/watcher/troubleshooting.asciidoc',
'en/rest-api/license/delete-license.asciidoc',
'en/rest-api/license/start-trial.asciidoc',
'en/rest-api/license/update-license.asciidoc',
'en/ml/api-quickref.asciidoc',
'en/rest-api/ml/delete-calendar-event.asciidoc',
Expand Down
687 changes: 687 additions & 0 deletions x-pack/docs/build.gradle.orig

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions x-pack/docs/en/rest-api/license/start-trial.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The following example checks whether you are eligible to start a trial:

[source,js]
------------------------------------------------------------
POST _xpack/license/start_trial
GET _xpack/license/start_trial
------------------------------------------------------------
// CONSOLE
// TEST[skip:license testing issues]
Expand All @@ -49,6 +49,27 @@ Example response:
[source,js]
------------------------------------------------------------
{
"trial_was_started": true
"eligible_to_start_trial": true
}
------------------------------------------------------------
// NOTCONSOLE

The following example starts a 30-day trial license. The acknowledge
parameter is required as you are initiating a license that will expire.

[source,js]
------------------------------------------------------------
POST _xpack/license/start_trial?acknowledge=true
------------------------------------------------------------
// CONSOLE
// TEST[skip:license testing issues]

Example response:
[source,js]
------------------------------------------------------------
{
"trial_was_started": true,
"acknowledged": true
}
------------------------------------------------------------
// NOTCONSOLE
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public void deleteLicense(DeleteLicenseRequest request, ActionListener<DeleteLic
client.execute(DeleteLicenseAction.INSTANCE, request, listener);
}

public PostStartTrialRequestBuilder preparePostUpgradeToTrial() {
public PostStartTrialRequestBuilder preparePostStartTrial() {
return new PostStartTrialRequestBuilder(client, PostStartTrialAction.INSTANCE);
}

public GetTrialStatusRequestBuilder prepareGetUpgradeToTrial() {
public GetTrialStatusRequestBuilder prepareGetStartTrial() {
return new GetTrialStatusRequestBuilder(client, GetTrialStatusAction.INSTANCE);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.rest.action.RestBuilderListener;

public class LicensingClient {

private final ElasticsearchClient client;

public LicensingClient(ElasticsearchClient client) {
this.client = client;
}

public PutLicenseRequestBuilder preparePutLicense(License license) {
return new PutLicenseRequestBuilder(client).setLicense(license);
}

public void putLicense(PutLicenseRequest request, ActionListener<PutLicenseResponse> listener) {
client.execute(PutLicenseAction.INSTANCE, request, listener);
}

public GetLicenseRequestBuilder prepareGetLicense() {
return new GetLicenseRequestBuilder(client);
}

public void getLicense(GetLicenseRequest request, ActionListener<GetLicenseResponse> listener) {
client.execute(GetLicenseAction.INSTANCE, request, listener);
}

public DeleteLicenseRequestBuilder prepareDeleteLicense() {
return new DeleteLicenseRequestBuilder(client);
}

public void deleteLicense(DeleteLicenseRequest request, ActionListener<DeleteLicenseResponse> listener) {
client.execute(DeleteLicenseAction.INSTANCE, request, listener);
}

public PostStartTrialRequestBuilder preparePostUpgradeToTrial() {
return new PostStartTrialRequestBuilder(client, PostStartTrialAction.INSTANCE);
}

public GetTrialStatusRequestBuilder prepareGetUpgradeToTrial() {
return new GetTrialStatusRequestBuilder(client, GetTrialStatusAction.INSTANCE);
}

public void postStartTrial(PostStartTrialRequest request, ActionListener<PostStartTrialResponse> listener) {
client.execute(PostStartTrialAction.INSTANCE, request, listener);
}

public void postStartBasic(PostStartBasicRequest request, ActionListener<PostStartBasicResponse> listener) {
client.execute(PostStartBasicAction.INSTANCE, request, listener);
}

public PostStartBasicRequestBuilder preparePostStartBasic() {
return new PostStartBasicRequestBuilder(client, PostStartBasicAction.INSTANCE);
}

public GetBasicStatusRequestBuilder prepareGetStartBasic() {
return new GetBasicStatusRequestBuilder(client, GetBasicStatusAction.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

public class PostStartTrialRequest extends MasterNodeRequest<PostStartTrialRequest> {

private boolean acknowledge = false;
private String type;

@Override
Expand All @@ -31,25 +32,47 @@ public String getType() {
return type;
}

public PostStartTrialRequest acknowledge(boolean acknowledge) {
this.acknowledge = acknowledge;
return this;
}

public boolean isAcknowledged() {
return acknowledge;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
type = in.readString();
acknowledge = in.readBoolean();
} else {
type = "trial";
acknowledge = true;
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
Version version = Version.V_6_3_0;
// TODO: Change to 6.3 after backport
Version version = Version.V_7_0_0_alpha1;
if (out.getVersion().onOrAfter(version)) {
super.writeTo(out);
out.writeString(type);
out.writeBoolean(acknowledge);
} else {
throw new IllegalArgumentException("All nodes in cluster must be version [" + version
+ "] or newer to use `type` parameter. Attempting to write to node with version [" + out.getVersion() + "].");
if ("trial".equals(type) == false) {
throw new IllegalArgumentException("All nodes in cluster must be version [" + version
+ "] or newer to start trial with a different type than 'trial'. Attempting to write to " +
"a node with version [" + out.getVersion() + "] with trial type [" + type + "].");
} else if (acknowledge == false) {
throw new IllegalArgumentException("Request must be acknowledged to send to a node with a version " +
"prior to [" + version + "]. Attempting to send request to node with version [" + out.getVersion() + "] " +
"without acknowledgement.");
} else {
super.writeTo(out);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;

public class PostStartTrialRequest extends MasterNodeRequest<PostStartTrialRequest> {

private String type;

@Override
public ActionRequestValidationException validate() {
return null;
}

public PostStartTrialRequest setType(String type) {
this.type = type;
return this;
}

public String getType() {
return type;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
type = in.readString();
} else {
type = "trial";
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
Version version = Version.V_6_3_0;
if (out.getVersion().onOrAfter(version)) {
out.writeString(type);
} else {
throw new IllegalArgumentException("All nodes in cluster must be version [" + version
+ "] or newer to use `type` parameter. Attempting to write to node with version [" + out.getVersion() + "].");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ class PostStartTrialRequestBuilder extends ActionRequestBuilder<PostStartTrialRe
PostStartTrialRequestBuilder(ElasticsearchClient client, PostStartTrialAction action) {
super(client, action, new PostStartTrialRequest());
}

public PostStartTrialRequestBuilder setAcknowledge(boolean acknowledge) {
request.acknowledge(acknowledge);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license;

import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;

class PostStartTrialRequestBuilder extends ActionRequestBuilder<PostStartTrialRequest,
PostStartTrialResponse, PostStartTrialRequestBuilder> {

PostStartTrialRequestBuilder(ElasticsearchClient client, PostStartTrialAction action) {
super(client, action, new PostStartTrialRequest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,33 @@
*/
package org.elasticsearch.license;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

class PostStartTrialResponse extends ActionResponse {

// Nodes Prior to 6.3 did not have NEED_ACKNOWLEDGEMENT as part of status
enum Pre63Status {
UPGRADED_TO_TRIAL,
TRIAL_ALREADY_ACTIVATED;
}
enum Status {
UPGRADED_TO_TRIAL(true, null, RestStatus.OK),
TRIAL_ALREADY_ACTIVATED(false, "Operation failed: Trial was already activated.", RestStatus.FORBIDDEN);
TRIAL_ALREADY_ACTIVATED(false, "Operation failed: Trial was already activated.", RestStatus.FORBIDDEN),
NEED_ACKNOWLEDGEMENT(false,"Operation failed: Needs acknowledgement.", RestStatus.OK);

private final boolean isTrialStarted;

private final String errorMessage;
private final RestStatus restStatus;

Status(boolean isTrialStarted, String errorMessage, RestStatus restStatus) {
this.isTrialStarted = isTrialStarted;
this.errorMessage = errorMessage;
Expand All @@ -39,15 +49,24 @@ String getErrorMessage() {
RestStatus getRestStatus() {
return restStatus;
}

}

private Status status;
private Map<String, String[]> acknowledgeMessages;
private String acknowledgeMessage;

PostStartTrialResponse() {
}

PostStartTrialResponse(Status status) {
this(status, Collections.emptyMap(), null);
}

PostStartTrialResponse(Status status, Map<String, String[]> acknowledgeMessages, String acknowledgeMessage) {
this.status = status;
this.acknowledgeMessages = acknowledgeMessages;
this.acknowledgeMessage = acknowledgeMessage;
}

public Status getStatus() {
Expand All @@ -57,10 +76,57 @@ public Status getStatus() {
@Override
public void readFrom(StreamInput in) throws IOException {
status = in.readEnum(Status.class);
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
acknowledgeMessage = in.readOptionalString();
int size = in.readVInt();
Map<String, String[]> acknowledgeMessages = new HashMap<>(size);
for (int i = 0; i < size; i++) {
String feature = in.readString();
int nMessages = in.readVInt();
String[] messages = new String[nMessages];
for (int j = 0; j < nMessages; j++) {
messages[j] = in.readString();
}
acknowledgeMessages.put(feature, messages);
}
this.acknowledgeMessages = acknowledgeMessages;
} else {
this.acknowledgeMessages = Collections.emptyMap();
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeEnum(status);
// TODO: Change to 6.3 after backport
Version version = Version.V_7_0_0_alpha1;
if (out.getVersion().onOrAfter(version)) {
out.writeEnum(status);
out.writeOptionalString(acknowledgeMessage);
out.writeVInt(acknowledgeMessages.size());
for (Map.Entry<String, String[]> entry : acknowledgeMessages.entrySet()) {
out.writeString(entry.getKey());
out.writeVInt(entry.getValue().length);
for (String message : entry.getValue()) {
out.writeString(message);
}
}
} else {
if (status == Status.UPGRADED_TO_TRIAL) {
out.writeEnum(Pre63Status.UPGRADED_TO_TRIAL);
} else if (status == Status.TRIAL_ALREADY_ACTIVATED) {
out.writeEnum(Pre63Status.TRIAL_ALREADY_ACTIVATED);
} else {
throw new IllegalArgumentException("Starting trial on node with version [" + Version.CURRENT + "] requires " +
"acknowledgement parameter.");
}
}
}

Map<String, String[]> getAcknowledgementMessages() {
return acknowledgeMessages;
}

String getAcknowledgementMessage() {
return acknowledgeMessage;
}
}
Loading

0 comments on commit a7629d9

Please sign in to comment.