Not able to connect to Capella Coachbase from local java code for trail cluster -SSL /TLS

42 views
Skip to first unread message

Tatineni Naga Lakshmi

unread,
Nov 7, 2022, 12:33:11 AM11/7/22
to Couchbase

Hi Team,

 

This is my java code which trying to connect to Capella instance using SSL/TLs

 

ERROR:

 

  1. The method connect(String, ClusterOptions) is undefined for the type Cluster
  2. The method queryIndexes() is undefined for the type Cluster
  3. The method createPrimaryQueryIndexOptions() is undefined for the type CoachbaseSecurity

 

 

 

JAVA CODE:

 

package cb;

import java.time.Duration;
import java.util.Collections;
import java.util.List;

import com.couchbase.client.core.error.DocumentNotFoundException;
import com.couchbase.client.java.Bucket;
//import com.couchbase.client.vbucket.config.Bucket;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.Scope;

import com.tibco.be.model.functions.BEPackage;


import com.couchbase.client.java.env.ClusterEnvironment;
import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.query.QueryResult;
import com.couchbase.client.java.query.QueryScanConsistency;
import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import com.couchbase.client.core.env.IoConfig;
import com.couchbase.client.core.env.SecurityConfig;
import com.couchbase.client.java.ClusterOptions;

import java.security.KeyStore;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions;
import com.couchbase.client.java.query.QueryOptions;
import static com.couchbase.client.java.query.QueryOptions.queryOptions;

@BEPackage(catalog = "VendorGatewayFunctions", category = "CB.Connect", synopsis = "coachbase")
public class CoachbaseSecurity {

    public static void main(String[] args) {

        // Update these variables to point to your Couchbase Capella instance and credentials.
        String endpoint = "couchbases://cb.dztziqcneqe9ilcn.cloud.couchbase.com";
        String bucketname = "travel-sample";
        String username = "tibcofinder";
        String password = "Krish234$";

        String tlsCertificate = "-----BEGIN CERTIFICATE-----\n" +"MIIDFTCCAf2gAwIBAgIRANLVkgOvtaXiQJi0V6qeNtswDQYJKoZIhvcNAQELBQAwJDESMBAGA1UECgwJQ291Y2hiYXNlMQ4wDAYDVQQLDAVDbG91ZDAeFw0xOTEyMDYyMjEyNTlaFw0yOTEyMDYyMzEyNTlaMCQxEjAQBgNVBAoMCUNvdWNoYmFzZTEOMAwGA1UECwwFQ2xvdWQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCfvOIienG4Dp+hJu9asdxEMRmH70hDyMXv5ZjBhbo39a42QwR59y/rC/sahLLQuNwqif85Fod1DkqgO6Ng3vecSAwyYVkj5NKdycQu5tzsZkghlpSDAyI0xlIPSQjoORA/pCOUWOpymA9dOjC1bo6rDyw0yWP2nFAI/KA4Z806XeqLREuB7292UnSsgFs4/5lqeil6rL3ooAw/i0uxr/TQSaxi1l8t4iMt4/gU+W52+8Yol0JbXBTFX6itg62ppb/EugmnmQRMgL67ccZs7cJ9/A0wlXencX2ohZQOR3mtknfol3FH4+glQFn27Q4xBCzVkY9jKQ20T1LgmGSngBInAgMBAAGjQjBAMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFJQOBPvrkU2In1Sjoxt97Xy8+cKNMA4GA1UdDwEB/wQEAwIBhjANBgkqhkiG9w0BAQsFAAOCAQEARgM6XwcXPLSpFdSf0w8PtpNGehmdWijPM3wHb7WZiS47iNen3oq8m2mm6V3Z57wbboPpfI+VEzbhiDcFfVnK1CXMC0tkF3fnOG1BDDvwt4jU95vBiNjYxdzlTP/Z+qr0cnVbGBSZ+fbXstSiRaaAVcqQyv3BRvBadKBkCyPwo+7svQnScQ5PJs7HEHKVms5tZTgKIw1fbmgR2XHleah1AcANB+MAPBCcTgqurqr5G7W2aPSBLLGAfRIiVzm7VFLc7kWbp7ENH39HVG6TZzKnfl9zJYeiklo5vQQhGSMhzBsO70z4RRziDPFAN/4qZAgD5q3AFNIq2WWADFQGSwVJhg=="+
          "-----END CERTIFICATE-----";
        // User Input ends here.

        // Configure TLS
        List<X509Certificate> cert = SecurityConfig.decodeCertificates(Collections.singletonList(tlsCertificate));
        SecurityConfig.Builder securityConfig = SecurityConfig
          .enableTls(true) // Enable transport security
          .trustCertificates(cert); // Configure the cloud certificate
          // During development, if you want to trust all certificates you can connect
          // to your cloud like with the InsecureTrustManagerFactory. As the name points
          // out, this is INSECURE!
          // .trustManagerFactory(InsecureTrustManagerFactory.INSTANCE)

        // Build the environment with the TLS config
        ClusterEnvironment env = ClusterEnvironment
          .builder()
          .securityConfig(securityConfig)
          .build();

        // Initialize the Connection
        Cluster cluster = Cluster.connect(endpoint, ClusterOptions.clusterOptions(username, password).environment(env));
        Bucket bucket = cluster.bucket(bucketname);
        bucket.waitUntilReady(Duration.parse("PT10S")) ;
        Collection collection = bucket.defaultCollection();

        // Create a JSON Document
        JsonObject arthur = JsonObject.create()
          .put("name", "Laxmi")
          .put("email", "La...@couchbase.com")
          .put("interests", JsonArray.from("Holy Grail", "African Swallows"));

        // Store the Document
        collection.upsert("u:king_arthur", arthur);

        // Load the Document and print it
        // Prints Content and Metadata of the stored Document
        System.out.println(collection.get("u:king_arthur"));

        // Create a N1QL Primary Index
    cluster.queryIndexes().createPrimaryIndex(
          bucketname,
          createPrimaryQueryIndexOptions().ignoreIfExists(true)
        ); 
        // Perform a N1QL Query
        QueryResult result = cluster.query(
          "SELECT name FROM " + bucketname + " WHERE $1 IN interests",
          queryOptions()
            .parameters(JsonArray.from("African Swallows"))
            .scanConsistency(QueryScanConsistency.REQUEST_PLUS)
        );

        // Print each found Row
        for (JsonObject row : result.rowsAsObject()) {
            System.out.println("Query row: " + row);
        }
    }

    
    

    

 


    

 


    Bucket bucket(String bucketName) {
        // TODO Auto-generated method stub
        return null;
    }

 


    

    
}

 

Perry Krug

unread,
Nov 8, 2022, 9:26:10 AM11/8/22
to Couchbase
Hi there.  A little while ago we launched our Discord Server to give you more real-time engagement with our engineers and the community, would you mind reposting this question there as this mailing list is being deprecated: https://www.couchbase.com/blog/couchbase-on-discord/

You can also post on the forums if you prefer: https://forums.couchbase.com/

Reply all
Reply to author
Forward
0 new messages