Search in sources :

Example 21 with MockContext

use of com.walmartlabs.concord.sdk.MockContext in project concord-plugins by walmartlabs.

the class PuppetTaskTest method testSelfSignedCertWithSecret.

@Test
public void testSelfSignedCertWithSecret() throws Exception {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    UtilsTest.injectVariable(task, "action", "pql");
    ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
    ctx.setVariable(Constants.Keys.DATABASE_URL_KEY, httpsRule.baseUrl());
    // Self-signed cert will fail unless we provide a cert to trust
    try {
        task.execute(ctx);
        fail("Task should fail when self-signed cert is used without a provided certificate to trust.");
    } catch (SSLHandshakeException expected) {
        // that's fine
        log.info("hit expected ssl exception. Not a problem");
    } catch (Exception e) {
        log.error(e.getMessage());
        throw e;
    }
    Map<String, Object> certificate = new HashMap<>();
    Map<String, Object> secret = new HashMap<>();
    secret.put(Constants.Keys.CERTIFICATE_ORG_KEY, "org");
    secret.put(Constants.Keys.CERTIFICATE_NAME_KEY, "name");
    secret.put(Constants.Keys.CERTIFICATE_PASSWORD_KEY, null);
    certificate.put("secret", secret);
    ctx.setVariable("certificate", certificate);
    // now it should work
    task.execute(ctx);
    Map result = (Map) ctx.getVariable("result");
    assertNotNull(result);
    assertTrue((boolean) result.get("ok"));
    List data = (List) result.get("data");
    assertEquals(10, data.size());
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) Test(org.junit.Test)

Example 22 with MockContext

use of com.walmartlabs.concord.sdk.MockContext in project concord-plugins by walmartlabs.

the class PuppetTaskTest method testQuery.

@Test
public void testQuery() throws Exception {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    UtilsTest.injectVariable(task, "action", "pql");
    ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
    task.execute(ctx);
    Map result = (Map) ctx.getVariable("result");
    assertNotNull(result);
    assertTrue((boolean) result.get("ok"));
    List data = (List) result.get("data");
    assertEquals(10, data.size());
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 23 with MockContext

use of com.walmartlabs.concord.sdk.MockContext in project concord-plugins by walmartlabs.

the class PuppetTaskTest method testSelfSignedCertWithText.

@Test
public void testSelfSignedCertWithText() throws Exception {
    MockContext ctx = new MockContext(buildDbQueryConfig());
    UtilsTest.injectVariable(task, "action", "pql");
    ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
    ctx.setVariable(Constants.Keys.DATABASE_URL_KEY, httpsRule.baseUrl());
    // Self-signed cert will fail unless we provide a cert to trust
    try {
        task.execute(ctx);
        fail("Task should fail when self-signed cert is used without a provided certificate to trust.");
    } catch (SSLHandshakeException expected) {
        // that's fine
        log.info("hit expected ssl exception. Not a problem");
    } catch (Exception e) {
        log.error(e.getMessage());
        throw e;
    }
    Map<String, Object> certificate = new HashMap<>();
    certificate.put("text", getWiremockCertString());
    ctx.setVariable("certificate", certificate);
    // now it should work
    task.execute(ctx);
    Map result = (Map) ctx.getVariable("result");
    assertNotNull(result);
    assertTrue((boolean) result.get("ok"));
    List data = (List) result.get("data");
    assertEquals(10, data.size());
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) Test(org.junit.Test)

Example 24 with MockContext

use of com.walmartlabs.concord.sdk.MockContext in project concord-plugins by walmartlabs.

the class PuppetTaskTest method testTokenCreate.

@Test
public void testTokenCreate() throws Exception {
    MockContext ctx = new MockContext(buildRbacCfg());
    UtilsTest.injectVariable(task, "action", "createApiToken");
    task.execute(ctx);
    Map result = (Map) ctx.getVariable("result");
    assertTrue((Boolean) result.get("ok"));
    String token = (String) result.get("data");
    assertNotNull(token);
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 25 with MockContext

use of com.walmartlabs.concord.sdk.MockContext in project concord-plugins by walmartlabs.

the class S3TaskTest method test.

@Test
@SuppressWarnings("unchecked")
public void test() throws Exception {
    String data = "Hello!";
    Path p = Files.createTempFile("test", ".txt");
    Files.write(p, data.getBytes());
    Map<String, Object> auth = new HashMap<>();
    auth.put("accessKey", System.getenv("AWS_ACCESS_KEY"));
    auth.put("secretKey", System.getenv("AWS_SECRET_KEY"));
    Map<String, Object> args = new HashMap<>();
    args.put(com.walmartlabs.concord.sdk.Constants.Context.WORK_DIR_KEY, p.getParent().toAbsolutePath().toString());
    args.put(ACTION_KEY, TaskParams.Action.PUTOBJECT.name());
    args.put(PutObjectParams.BUCKET_NAME_KEY, "ibodrov-test");
    args.put(PutObjectParams.OBJECT_KEY, "xyz");
    args.put(PutObjectParams.SRC_KEY, p.getFileName().toString());
    args.put(PutObjectParams.ENDPOINT_KEY, System.getenv("AWS_ENDPOINT"));
    args.put(PutObjectParams.REGION_KEY, System.getenv("AWS_REGION"));
    args.put(PutObjectParams.PATH_STYLE_ACCESS_KEY, true);
    args.put(PutObjectParams.AUTH_KEY, Collections.singletonMap("basic", auth));
    MockContext ctx = new MockContext(args);
    Task t = new S3Task();
    t.execute(ctx);
    Map<String, Object> r = (Map<String, Object>) ctx.getVariable(Constants.RESULT_KEY);
    assertTrue((Boolean) r.get("ok"));
    // ---
    args.put(Constants.ACTION_KEY, TaskParams.Action.GETOBJECT.name());
    ctx = new MockContext(args);
    t.execute(ctx);
    r = (Map<String, Object>) ctx.getVariable(Constants.RESULT_KEY);
    assertTrue((Boolean) r.get("ok"));
    String storedObject = (String) r.get("path");
    String s = new String(Files.readAllBytes(p.getParent().resolve(storedObject)));
    assertEquals(data, s);
}
Also used : Path(java.nio.file.Path) Task(com.walmartlabs.concord.sdk.Task) MockContext(com.walmartlabs.concord.sdk.MockContext) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

MockContext (com.walmartlabs.concord.sdk.MockContext)25 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 Map (java.util.Map)11 ApiException (com.walmartlabs.concord.plugins.puppet.model.exception.ApiException)8 MissingParameterException (com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException)8 CertificateException (java.security.cert.CertificateException)8 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 Context (com.walmartlabs.concord.sdk.Context)3 SmtpServer (com.icegreen.greenmail.smtp.SmtpServer)2 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 File (java.io.File)2 Path (java.nio.file.Path)2 MimeMessage (javax.mail.internet.MimeMessage)2 Before (org.junit.Before)2 RbacCfg (com.walmartlabs.concord.plugins.puppet.model.cfg.RbacCfg)1 TokenPayload (com.walmartlabs.concord.plugins.puppet.model.token.TokenPayload)1 LockService (com.walmartlabs.concord.sdk.LockService)1 ObjectStorage (com.walmartlabs.concord.sdk.ObjectStorage)1