Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

  • If RepoTool is installed for the first time on the server follow the below steps:

    1. Create an admin account: docker exec -it debbie-django python manage.py createsuperuser

    2. Enter the username and password for the admin account (keep the username and password the same as Mongo for better accessibility).

    3. Go to http://<server-ip>:8000/admin and login using the credentials entered above to login.

    4. After login select the API keys option and select the ADD API KEY button highlighted below.

    5. Copy the API key generated and save it as it will be only displayed once.

    6. There is also an alternative for steps c,d, and e if those don’t work in case URL is inaccessible:

      1. SSH to the VM where KnowHOW or RepoTool is installed and then in the console enter this cmd docker exec -it debbie-django python manage.py shell_plus --ipython

      2. Once the python shell opens enter the following code:

        from rest_framework_api_key.models import APIKey
        api_key, key = APIKey.objects.create_key(name="my-key")
        print(key)
      3. The above code will give the API key as output, copy it

    7. In the env file of RepoTool /app/apps/.env, paste the below lines:
      BASE_HOST_URL=https://4bdf-89-136-52-54.ngrok-free.app
      DEBBIE_INTERNAL_API_KEY=<enter the copied API key>
      SCAN_STATUS_APIS=http://customapi:8080/api/processor/saveRepoToolsStatus

    8. Stop the containers using docker stop debbie-django debbie-knowhow and docker rm debbie-django debbie-knowhow.

    9. Start using docker-compose up -d debbie-django debbie-knowhow.

    10. Encrypt the generated API KEY using the code snippet below:

      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.nio.charset.StandardCharsets;
      import java.security.InvalidAlgorithmParameterException;
      import java.security.InvalidKeyException;
      import java.security.NoSuchAlgorithmException;
      import java.security.SecureRandom;
      import java.security.spec.InvalidKeySpecException;
      import java.security.spec.KeySpec;
      import java.util.Base64;
      import javax.crypto.BadPaddingException;
      import javax.crypto.Cipher;
      import javax.crypto.IllegalBlockSizeException;
      import javax.crypto.NoSuchPaddingException;
      import javax.crypto.SecretKey;
      import javax.crypto.SecretKeyFactory;
      import javax.crypto.spec.IvParameterSpec;
      import javax.crypto.spec.PBEKeySpec;
      import javax.crypto.spec.SecretKeySpec;
      
      
      
      public class Main {
      
          private static final String ALGO = "AES";
          private static final String DEFAULT_MODE_AND_PADDING_SCHEME = "AES/CBC/PKCS5Padding";
          private static final int ITERATION_COUNT = 65536;
          private static final int KEY_LENGTH = 256;
          private static final String KEY_INSTANCE = "PBKDF2WithHmacSHA1";
      
          private static byte[] generateSalt() {
              byte[] salt = new byte[16];
              new SecureRandom().nextBytes(salt);
              return salt;
          }
      
          private static IvParameterSpec generateIv() {
              byte[] iv = new byte[16];
              new SecureRandom().nextBytes(iv);
              return new IvParameterSpec(iv);
          }
      
          public static String encrypt(String text, String key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
      
              byte[] salt = generateSalt();
              SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_INSTANCE);
              KeySpec spec = new PBEKeySpec(key.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
              SecretKey tempKey = factory.generateSecret(spec);
              SecretKey secret = new SecretKeySpec(tempKey.getEncoded(), ALGO);
              Cipher cipher = Cipher.getInstance(DEFAULT_MODE_AND_PADDING_SCHEME);// NOSONAR
              IvParameterSpec iv = generateIv();
              cipher.init(Cipher.ENCRYPT_MODE, secret, iv);
              byte[] encryptedText = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
              ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
              outputStream.write(salt);
              outputStream.write(iv.getIV());
              outputStream.write(encryptedText);
              return Base64.getEncoder().encodeToString(outputStream.toByteArray());
          }
      
      
          public static void main(String[] args) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeySpecException, BadPaddingException, IOException, InvalidKeyException {
              System.out.println(encrypt("<API-Key-here>", "<Customapi AES Encryption key>"));
          }
      }
    11. And paste it in /app/apps/properties/customapi.properties file of server as

      repoToolAPIKey=<encrypted-api-key-here>
    12. Restart the server docker restart customapi.

  • No labels