Forum Discussion

Vincent_Li_9688's avatar
Vincent_Li_9688
Historic F5 Account
Mar 12, 2013

iControl Java help on certificate_import_from_pem

I am totally Java and iControl Java API newbie, and this is my first Java code adopted from devcental, I am trying to import certificate use certificate_import_from_pem, I got an highlight error in Eclipse, I think i am not getting the object definition/declaration from iControl class correct, any tip ?

 

 

public class KeyCertificate {

 

 

public iControl.Interfaces m_interfaces = new iControl.Interfaces();

 

 

public void usage()

 

{

 

System.out.println("Usage: SystemInfo hostname username password");

 

}

 

 

public void Run(String [] args) throws Exception

 

{

 

if ( args.length < 3 )

 

{

 

usage();

 

}

 

else

 

{

 

String host = args[0];

 

String user = args[1];

 

String pass = args[2];

 

 

boolean bInit = m_interfaces.initialize(host, user, pass);

 

if ( bInit )

 

{

 

addCertificates();

 

}

 

}

 

}

 

///////////////////////////////////////// private method for the cert //////////////////////////////////////////////////////////////

 

 

private void addCertificates()

 

throws Exception

 

{

 

 

 

String[] certs = new String[1];

 

String names = "test_cert";

 

String mode = "ManagementKeyCertificateManagementModeType.MANAGEMENT_MODE_DEFAULT"; ///////////////here seems not right

 

 

iControl.ManagementKeyCertificateBindingStub certstub =

 

m_interfaces.getManagementKeyCertificate();

 

 

certs[0] = "-----BEGIN CERTIFICATE-----\n" +

 

"MIICbjCCAdcCBgEKka0UIzANBgkqhkiG9w0BAQsFADB9MQswCQYDVQQGEwJBVTEM\n" +

 

"MAoGA1UECBMDTlNXMQ8wDQYDVQQHEwZTeWRuZXkxGTAXBgNVBAoTEHBpbXBqaW1t\n" +

 

"eS5hdS5jb20xGTAXBgNVBAsTEHBpbXBqaW1teS5hdS5jb20xGTAXBgNVBAMTEHBp\n" +

 

"bXBqaW1teS5hdS5jb20wHhcNMDYwNDEzMDUxNTM0WhcNMTEwNDEzMDUxNTM5WjB9\n" +

 

"MQswCQYDVQQGEwJBVTEMMAoGA1UECBMDTlNXMQ8wDQYDVQQHEwZTeWRuZXkxGTAX\n" +

 

"BgNVBAoTEHBpbXBqaW1teS5hdS5jb20xGTAXBgNVBAsTEHBpbXBqaW1teS5hdS5j\n" +

 

"b20xGTAXBgNVBAMTEHBpbXBqaW1teS5hdS5jb20wgZ8wDQYJKoZIhvcNAQEBBQAD\n" +

 

"gY0AMIGJAoGBANgR1vv312gokkkQzotDS1fqXV57HcgVeMA/dHetaqbWNFnvF6pi\n" +

 

"+U+d85sh9ONwLAv8vrU58yUZFnEcwc1mp6P9O8MI6pAyCYACpdnVGkzQm7uDiFLr\n" +

 

"aKZiaMI3s4K2WA7kLR6T53oZlGx8LFIkU2rHygk59BW63zOiveQKaRHNAgMBAAEw\n" +

 

"DQYJKoZIhvcNAQELBQADgYEAaHGGQOHhf7oRhf2bZWcPH0wI/a6yJQ0zxegAnx9O\n" +

 

"1S8NeE/Cc+T+scQ2t3H8/wH/bxNlXdYVgo/F9oc7XNQ/KO6ainQbaH37irG2WBvM\n" +

 

"69N+ZA8xGS9rPen4za7YqNzshDBa1ZOIjh5uHw2UCj5C/vwhGefVaeTxkmXHtQVt\n" +

 

"BHY=\n" +

 

"-----END CERTIFICATE-----";

 

 

certstub.certificate_import_from_pem(mode, names, certs, true); ///////////////////////got an error here in Elicpse

 

}

 

 

 

 

/**

 

* @param args

 

*/

 

public static void main(String[] args) {

 

try

 

{

 

KeyCertificate kc = new KeyCertificate();

 

kc.Run(args);

 

}

 

catch(Exception ex)

 

{

 

ex.printStackTrace(System.out);

 

}

 

}

 

 

}

 

2 Replies

  • 1. "names" should be an array and not a string - read the API documentation: https://devcentral.f5.com/wiki/iControl.Management__KeyCertificate__certificate_import_from_pem.ashx

     

    2. "modes" is not a string either :-) documentation in the wiki link above

     

    3. learn Java before attempting to program in that language (or choose a programming language you know best - iControl works with many languages)

     

  • Vincent_Li_9688's avatar
    Vincent_Li_9688
    Historic F5 Account

    FYI, I end up with following java code to make it work:

     

     

    import java.io.*;

     

    import java.util.Scanner;

     

     

    public class KeyCertificate {

     

     

    public iControl.Interfaces m_interfaces = new iControl.Interfaces();

     

     

    public void usage()

     

    {

     

    System.out.println("Usage: KeyCertificate hostname username password");

     

    }

     

     

    public void Run(String [] args) throws Exception

     

    {

     

    if ( args.length < 3 )

     

    {

     

    usage();

     

    }

     

    else

     

    {

     

    String host = args[0];

     

    String user = args[1];

     

    String pass = args[2];

     

     

    boolean bInit = m_interfaces.initialize(host, user, pass);

     

    if ( bInit )

     

    {

     

    addCertificates();

     

     

    }

     

    }

     

    }

     

     

    private String readFile(String pathname) throws IOException {

     

     

    File file = new File(pathname);

     

    StringBuilder fileContents = new StringBuilder((int)file.length());

     

    Scanner scanner = new Scanner(file);

     

    String lineSeparator = System.getProperty("line.separator");

     

     

    try {

     

    while(scanner.hasNextLine()) {

     

    fileContents.append(scanner.nextLine() + lineSeparator);

     

    }

     

    return fileContents.toString();

     

    } finally {

     

    scanner.close();

     

    }

     

    }

     

     

     

     

    private void addCertificates()

     

    throws Exception

     

    {

     

     

     

    String[] certs = new String[1];

     

    String[] keys = new String[1];

     

    String[] key_ids = null;

     

    key_ids = new String[]{"vli_self_server_key"};

     

    String[] cert_ids = null;

     

    cert_ids = new String[]{"vli_self_server_cert"};

     

    iControl.ManagementKeyCertificateBindingStub certstub = m_interfaces.getManagementKeyCertificate();

     

    iControl.ManagementKeyCertificateManagementModeType mode = iControl.ManagementKeyCertificateManagementModeType.MANAGEMENT_MODE_DEFAULT;

     

     

    keys[0] = readFile("C:\\Users\\vli\\iControl-java\\vli_self_server.key");

     

    certs[0] = readFile("C:\\Users\\vli\\iControl-java\\vli_self_server.crt");

     

     

    try {

     

    certstub.key_delete(mode, key_ids);

     

    certstub.certificate_delete(mode, cert_ids);

     

    }catch (Exception e) {

     

    System.out.println("certificate and key not exist");

     

    }

     

     

     

    certstub.certificate_import_from_pem(mode, cert_ids, certs, true);

     

    certstub.key_import_from_pem(mode, key_ids, keys, true);

     

    certstub.certificate_bind(mode, cert_ids, key_ids);

     

    System.out.println("certificate and key import succeeded!");

     

     

    }

     

     

     

     

    /**

     

    * @param args

     

    */

     

    public static void main(String[] args) {

     

    try

     

    {

     

    KeyCertificate kc = new KeyCertificate();

     

    kc.Run(args);

     

    }

     

    catch(Exception ex)

     

    {

     

    ex.printStackTrace(System.out);

     

    }

     

    }

     

     

    }