Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ jobs:
ref: '1.x'
- name: Build OpenSearch
working-directory: ./OpenSearch
run: ./gradlew publishToMavenLocal -Dbuild.snapshot=false
run: ./gradlew publishToMavenLocal

# common-utils
- name: Build and Test
run: |
./gradlew build -Dopensearch.version=1.1.0

./gradlew build -Dopensearch.version=1.1.0-SNAPSHOT

- name: Publish to Maven Local
run: |
./gradlew publishToMavenLocal -Dopensearch.version=1.1.0
./gradlew publishToMavenLocal -Dopensearch.version=1.1.0-SNAPSHOT

- name: Upload Coverage Report
uses: codecov/codecov-action@v1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img src="https://opensearch.org/assets/brand/SVG/Logo/opensearch_logo_default.svg" height="64px"/>
<img src="https://opensearch.org/assets/img/opensearch-logo-themed.svg" height="64px">

- [OpenSearch Common Utils](#opensearch-common-utils)
- [Contributing](#contributing)
Expand Down
15 changes: 13 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
buildscript {
ext {
opensearch_group = "org.opensearch"
opensearch_version = System.getProperty("opensearch.version", "1.1.0")

opensearch_version = System.getProperty("opensearch.version", "1.1.0-SNAPSHOT")
kotlin_version = System.getProperty("kotlin.version", "1.4.32")
}

Expand Down Expand Up @@ -44,7 +45,17 @@ repositories {
jcenter()
}

group 'org.opensearch.commons'
ext {
isSnapshot = "true" == System.getProperty("build.snapshot", "true")
}

allprojects {
group 'org.opensearch.commons'
version = opensearch_version - '-SNAPSHOT' + '.0'
if (isSnapshot) {
version += "-SNAPSHOT"
}
}

sourceCompatibility = 1.8

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/opensearch/commons/InjectSecurity.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ public void injectRoles(final List<String> roles) {
}
}

/**
* Allows one to set the property in threadContext if possible to the value provided. If not possible returns false.
* @param property
* @param value
* @return boolean
*/
public boolean injectProperty(final String property, final Object value) {
if (Strings.isNullOrEmpty(property) || value == null || threadContext.getTransient(property) != null) {
log.debug("{}, InjectSecurity - cannot inject property: {}", Thread.currentThread().getName(), id);
return false;
} else {
threadContext.putTransient(property, value);
log.debug("{}, InjectSecurity - inject property: {}", Thread.currentThread().getName(), id);
return true;
}
}

@Override
public void close() {
if (ctx != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.opensearch.commons.destination.message;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import org.apache.http.client.utils.URIBuilder;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;

/**
* This class holds the generic parameters required for a
* message.
*/
public abstract class LegacyBaseMessage implements Writeable {

private final LegacyDestinationType destinationType;
protected String destinationName;
protected String url;
private final String content;

LegacyBaseMessage(final LegacyDestinationType destinationType, final String destinationName, final String content) {
if (destinationType == null) {
throw new IllegalArgumentException("Channel type must be defined");
}
if (!Strings.hasLength(destinationName)) {
throw new IllegalArgumentException("Channel name must be defined");
}
this.destinationType = destinationType;
this.destinationName = destinationName;
this.content = content;
}

LegacyBaseMessage(final LegacyDestinationType destinationType, final String destinationName, final String content, final String url) {
this(destinationType, destinationName, content);
if (url == null) {
throw new IllegalArgumentException("url is invalid or empty");
}
this.url = url;
}

LegacyBaseMessage(StreamInput streamInput) throws IOException {
this.destinationType = streamInput.readEnum(LegacyDestinationType.class);
this.destinationName = streamInput.readString();
this.url = streamInput.readOptionalString();
this.content = streamInput.readString();
}

public void setUrl(String url) {
this.url = url;
}

public LegacyDestinationType getChannelType() {
return destinationType;
}

public String getChannelName() {
return destinationName;
}

public String getMessageContent() {
return content;
}

public String getUrl() {
return url;
}

public URI getUri() {
return buildUri(getUrl().trim(), null, null, -1, null, null);
}

protected URI buildUri(String endpoint, String scheme, String host, int port, String path, Map<String, String> queryParams) {
try {
if (Strings.isNullOrEmpty(endpoint)) {
if (Strings.isNullOrEmpty(scheme)) {
scheme = "https";
}
URIBuilder uriBuilder = new URIBuilder();
if (queryParams != null) {
for (Map.Entry<String, String> e : queryParams.entrySet())
uriBuilder.addParameter(e.getKey(), e.getValue());
}
return uriBuilder.setScheme(scheme).setHost(host).setPort(port).setPath(path).build();
}
return new URIBuilder(endpoint).build();
} catch (URISyntaxException exception) {
throw new IllegalStateException("Error creating URI");
}
}

@Override
public void writeTo(StreamOutput streamOutput) throws IOException {
streamOutput.writeEnum(destinationType);
streamOutput.writeString(destinationName);
streamOutput.writeOptionalString(url);
streamOutput.writeString(content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.opensearch.commons.destination.message;

import java.io.IOException;

import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;

/**
* This class holds the contents of an Chime message
*/
public class LegacyChimeMessage extends LegacyBaseMessage {
private final String message;

private LegacyChimeMessage(final String destinationName, final String url, final String message) {
super(LegacyDestinationType.LEGACY_CHIME, destinationName, message, url);

if (Strings.isNullOrEmpty(message)) {
throw new IllegalArgumentException("Message content is missing");
}

this.message = message;
}

public LegacyChimeMessage(StreamInput streamInput) throws IOException {
super(streamInput);
this.message = super.getMessageContent();
}

@Override
public String toString() {
return "DestinationType: " + getChannelType() + ", DestinationName:" + destinationName + ", Url: " + url + ", Message: <...>";
}

public static class Builder {
private String message;
private final String destinationName;
private String url;

public Builder(String destinationName) {
this.destinationName = destinationName;
}

public LegacyChimeMessage.Builder withMessage(String message) {
this.message = message;
return this;
}

public LegacyChimeMessage.Builder withUrl(String url) {
this.url = url;
return this;
}

public LegacyChimeMessage build() {
return new LegacyChimeMessage(this.destinationName, this.url, this.message);
}
}

public String getMessage() {
return message;
}

public String getUrl() {
return url;
}
}
Loading