Skip to content

Add support for IAM Auth and LTv3 #9

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

Merged
merged 6 commits into from
Jun 22, 2018
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
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.0
current_version = 0.2.0
commit = True
tag = True

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ All you need to do is:
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>watson-spring-boot-starter</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
</dependency>
```

or in your gradle `build.gradle`, in the dependencies stanza, add
```
compile 'com.ibm.watson.developer_cloud:watson-spring-boot-starter:0.1.0'
compile 'com.ibm.watson.developer_cloud:watson-spring-boot-starter:0.2.0'
```

2. Add your Watson service(s) credentials and version info to your application
Expand All @@ -36,7 +36,7 @@ use a different location. The properties to add are:

- `watson.<service>.url`: The base URL for the service. This can be omitted if your
service uses the default service url
- `watson.<service>.username` and `watson.<service>.password` OR `watson.<service>.apiKey`:
- `watson.<service>.username` and `watson.<service>.password` OR `watson.<service>.apiKey` OR `watson.<service>.iamApiKey`:
The credentials for accessing the service.
The credentials can be omitted from the application properties file if they are
supplied through the `VCAP_SERVICES` environment variable.
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version=0.1.0
version=0.2.0
group = com.ibm.watson.developer_cloud
watsonVersion = 4.2.1
watsonVersion = 6.1.0
springVersion = 4.3.12.RELEASE
springBootVersion = 1.5.9.RELEASE
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Tue Jan 02 10:02:13 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright © 2017 IBM Corp. 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. You may obtain a copy of the License at
*
* http://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 com.ibm.watson.developer_cloud.spring.boot;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = WatsonAssistantConfigurationProperties.PREFIX)
public class WatsonAssistantConfigurationProperties extends WatsonConfigurationProperties {

public static final String PREFIX = "watson.assistant";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

package com.ibm.watson.developer_cloud.spring.boot;

import com.ibm.watson.developer_cloud.assistant.v1.Assistant;
import com.ibm.watson.developer_cloud.conversation.v1.Conversation;
import com.ibm.watson.developer_cloud.discovery.v1.Discovery;
import com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator;
import com.ibm.watson.developer_cloud.language_translator.v3.LanguageTranslator;
import com.ibm.watson.developer_cloud.natural_language_classifier.v1.NaturalLanguageClassifier;
import com.ibm.watson.developer_cloud.natural_language_understanding.v1.NaturalLanguageUnderstanding;
import com.ibm.watson.developer_cloud.personality_insights.v3.PersonalityInsights;
import com.ibm.watson.developer_cloud.service.WatsonService;
import com.ibm.watson.developer_cloud.service.security.IamOptions;
import com.ibm.watson.developer_cloud.speech_to_text.v1.SpeechToText;
import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech;
import com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer;
Expand All @@ -34,6 +36,7 @@

@Configuration
@EnableConfigurationProperties({
WatsonAssistantConfigurationProperties.class,
WatsonConversationConfigurationProperties.class,
WatsonDiscoveryConfigurationProperties.class,
WatsonLanguageTranslatorConfigurationProperties.class,
Expand All @@ -54,21 +57,41 @@ private void configUrl(WatsonService service, WatsonConfigurationProperties conf
}
}

private void configBasicAuth(WatsonService service, WatsonConfigurationProperties config) {
private void configAuth(WatsonService service, WatsonConfigurationProperties config) {
String iamApiKey = config.getIamApiKey();
if (iamApiKey != null) {
IamOptions options = new IamOptions.Builder().apiKey(iamApiKey).build();
service.setIamCredentials(options);
return;
}
String username = config.getUsername();
String password = config.getPassword();
if (username != null && password != null) {
service.setUsernameAndPassword(username, password);
return;
}
}

private void configApiKey(WatsonService service, WatsonConfigurationProperties config) {
String apiKey = config.getApiKey();
if (apiKey != null) {
service.setApiKey(apiKey);
return;
}
}

// Watson Assistant service

@Autowired
private WatsonAssistantConfigurationProperties assistantConfig;

@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonAssistantConfigurationProperties.PREFIX)
public Assistant assistant() {
Assistant service = new Assistant(assistantConfig.getVersionDate());
configUrl(service, assistantConfig);
configAuth(service, assistantConfig);
return service;
}

// Watson Conversation service

@Autowired
Expand All @@ -80,7 +103,7 @@ private void configApiKey(WatsonService service, WatsonConfigurationProperties c
public Conversation conversation() {
Conversation service = new Conversation(conversationConfig.getVersionDate());
configUrl(service, conversationConfig);
configBasicAuth(service, conversationConfig);
configAuth(service, conversationConfig);
return service;
}

Expand All @@ -95,7 +118,7 @@ public Conversation conversation() {
public Discovery discovery() {
Discovery service = new Discovery(discoveryConfig.getVersionDate());
configUrl(service, discoveryConfig);
configBasicAuth(service, discoveryConfig);
configAuth(service, discoveryConfig);
return service;
}

Expand All @@ -108,9 +131,9 @@ public Discovery discovery() {
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonLanguageTranslatorConfigurationProperties.PREFIX)
public LanguageTranslator languageTranslator() {
LanguageTranslator service = new LanguageTranslator();
LanguageTranslator service = new LanguageTranslator(ltConfig.getVersionDate());
configUrl(service, ltConfig);
configBasicAuth(service, ltConfig);
configAuth(service, ltConfig);
return service;
}

Expand All @@ -125,7 +148,7 @@ public LanguageTranslator languageTranslator() {
public NaturalLanguageClassifier naturalLanguageClassifier() {
NaturalLanguageClassifier service = new NaturalLanguageClassifier();
configUrl(service, nlcConfig);
configBasicAuth(service, nlcConfig);
configAuth(service, nlcConfig);
return service;
}

Expand All @@ -140,7 +163,7 @@ public NaturalLanguageClassifier naturalLanguageClassifier() {
public NaturalLanguageUnderstanding naturalLanguageUnderstanding() {
NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(nluConfig.getVersionDate());
configUrl(service, nluConfig);
configBasicAuth(service, nluConfig);
configAuth(service, nluConfig);
return service;
}

Expand All @@ -155,7 +178,7 @@ public NaturalLanguageUnderstanding naturalLanguageUnderstanding() {
public PersonalityInsights personalityInsights() {
PersonalityInsights service = new PersonalityInsights(piConfig.getVersionDate());
configUrl(service, piConfig);
configBasicAuth(service, piConfig);
configAuth(service, piConfig);
return service;
}

Expand All @@ -170,7 +193,7 @@ public PersonalityInsights personalityInsights() {
public SpeechToText speechToText() {
SpeechToText service = new SpeechToText();
configUrl(service, sttConfig);
configBasicAuth(service, sttConfig);
configAuth(service, sttConfig);
return service;
}

Expand All @@ -185,7 +208,7 @@ public SpeechToText speechToText() {
public TextToSpeech textToSpeech() {
TextToSpeech service = new TextToSpeech();
configUrl(service, ttsConfig);
configBasicAuth(service, ttsConfig);
configAuth(service, ttsConfig);
return service;
}

Expand All @@ -200,7 +223,7 @@ public TextToSpeech textToSpeech() {
public ToneAnalyzer toneAnalyzer() {
ToneAnalyzer service = new ToneAnalyzer(taConfig.getVersionDate());
configUrl(service, taConfig);
configBasicAuth(service, taConfig);
configAuth(service, taConfig);
return service;
}

Expand All @@ -215,8 +238,7 @@ public ToneAnalyzer toneAnalyzer() {
public VisualRecognition visualRecognition() {
VisualRecognition service = new VisualRecognition(vrConfig.getVersionDate());
configUrl(service, vrConfig);
configBasicAuth(service, vrConfig);
configApiKey(service, vrConfig);
configAuth(service, vrConfig);
return service;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class WatsonConfigurationProperties {
/** Watson service API key. */
private String apiKey;

/** Watson service IAM API key. */
private String iamApiKey;

/** Watson service versionDate. */
private String versionDate;

Expand All @@ -47,6 +50,10 @@ public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public void setIamApiKey(String apiKey) {
this.iamApiKey = apiKey;
}

public void setVersionDate(String versionDate) {
this.versionDate = versionDate;
}
Expand All @@ -67,6 +74,10 @@ public String getApiKey() {
return apiKey;
}

public String getIamApiKey() {
return iamApiKey;
}

public String getVersionDate() {
return this.versionDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata
String url = conditionContext.getEnvironment().getProperty(prefix + ".url");
String username = conditionContext.getEnvironment().getProperty(prefix + ".username");
String password = conditionContext.getEnvironment().getProperty(prefix + ".password");
String apiKey = conditionContext.getEnvironment().getProperty(prefix + ".apiKey");
String iamApiKey = conditionContext.getEnvironment().getProperty(prefix + ".iamApiKey");
String versionDate = conditionContext.getEnvironment().getProperty(prefix + ".versionDate");
if (url != null || username != null || password != null || versionDate != null) {
if (url != null || username != null || password != null || versionDate != null
|| apiKey != null || iamApiKey != null) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright © 2017 IBM Corp. 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. You may obtain a copy of the License at
*
* http://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 com.ibm.watson.developer_cloud.spring.boot.test;

import com.ibm.watson.developer_cloud.assistant.v1.Assistant;
import com.ibm.watson.developer_cloud.service.WatsonService;
import com.ibm.watson.developer_cloud.spring.boot.WatsonAutoConfiguration;
import okhttp3.Credentials;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import java.lang.reflect.Field;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WatsonAutoConfiguration.class}, loader = AnnotationConfigContextLoader.class)
@TestPropertySource(properties = {
"watson.assistant.url=" + AssistantAutoConfigTest.url,
"watson.assistant.username=" + AssistantAutoConfigTest.username,
"watson.assistant.password=" + AssistantAutoConfigTest.password,
"watson.assistant.versionDate=" + AssistantAutoConfigTest.versionDate
})
public class AssistantAutoConfigTest {

static final String url = "http://watson.com/assistant";
static final String username = "sam";
static final String password = "secret";
static final String versionDate = "2017-12-15";

@Autowired
private ApplicationContext applicationContext;

@Test
public void assistantBeanConfig() {
Assistant assistant = (Assistant) applicationContext.getBean("assistant");

assertNotNull(assistant);
assertEquals(url, assistant.getEndPoint());

// Verify the credentials and versionDate -- which are stored in private member variables
try {
Field apiKeyField = WatsonService.class.getDeclaredField("apiKey");
apiKeyField.setAccessible(true);
assertEquals(Credentials.basic(username, password), apiKeyField.get(assistant));

Field versionField = Assistant.class.getDeclaredField("versionDate");
versionField.setAccessible(true);
assertEquals(versionDate, versionField.get(assistant));
} catch (NoSuchFieldException | IllegalAccessException ex) {
// This shouldn't happen
assert false;
}
}
}
Loading