Skip to content

Commit

Permalink
release script
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <[email protected]>
  • Loading branch information
daizhenyu committed Dec 13, 2024
1 parent 283eaf7 commit ccdc520
Show file tree
Hide file tree
Showing 6 changed files with 516 additions and 3 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
jobs:
auto-create-release:
name: Auto Create Release
runs-on: windows-latest
runs-on: ubuntu-latest
steps:
- name: save env version
shell: bash
Expand All @@ -32,9 +32,23 @@ jobs:
- name: set version
run: |
mvn versions:set -DnewVersion='${{ env.version }}'
- name: Build with Maven
- name: Build agent with Maven
run: |
mvn clean package -P agent -P package -DskipTests
mvn clean package -P agent -DskipTests
- name: compile tools
run: |
cd ${{ github.workspace }}/scripts
javac -cp ./:$JAVA_HOME/lib/tools.jar AgentLoader.java
javac AesUtil.java
gcc attach_sermant_agent.c -o attach_sermant_agent
mkdir ${{ github.workspace }}/sermant-agent-${{ env.version }}/tools
cp AgentLoader.class ${{ github.workspace }}/sermant-agent-${{ env.version }}/tools
cp AesUtil.class ${{ github.workspace }}/sermant-agent-${{ env.version }}/tools
cp attach_sermant_agent ${{ github.workspace }}/sermant-agent-${{ env.version }}/tools
cp readme.txt ${{ github.workspace }}/sermant-agent-${{ env.version }}/tools
- name: package sermant
run: |
mvn package -P package -DskipTests
mkdir ${{ github.workspace }}/package
cp ${{ github.workspace }}/sermant-agent-*.tar.gz ${{ github.workspace }}/package/sermant-${{ env.version }}.tar.gz
ls ${{ github.workspace }}/package
Expand Down
Binary file removed scripts/AesUtil.class
Binary file not shown.
110 changes: 110 additions & 0 deletions scripts/AesUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. 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.
*/

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Base64;
import java.util.Optional;
import java.util.Scanner;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
* sermant agent hot plugging script
*
* @author zhanghaopeng
* @since 2024-12-10
*/
public class AesUtil {
private static final int SIZE = 256;

private static final int LENGTH = 16;

private static final int IV_LENGTH = 12;

private static final String ALGORITHM = "AES";

private static final String AES_PADDING = "AES/GCM/PKCS5Padding";

private static final String DEFAULT_ENCODE = "UTF-8";

private AesUtil() {
}

public static void main(String[] var0) {
Scanner scanner = new Scanner(System.in);
System.out.print("please input your password\n");
String password = scanner.nextLine();
scanner.close();
Optional<String> keyOptional = generateKey();
if (!keyOptional.isPresent()) {
System.out.println("failed to generate key");
} else {
String key = (String) keyOptional.get();
System.out.println("encryption key is " + key);
Optional encryptedPassword = encrypt(key, password);
if (!encryptedPassword.isPresent()) {
System.out.println("failed to encrypted password");
} else {
System.out.println("encrypted password is " + (String) encryptedPassword.get());
}
}
}

/**
* Generate key pair
*
* @return key pair
*/
public static Optional<String> generateKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(SIZE);
SecretKey secretKey = keyGenerator.generateKey();
String keys = new String(Base64.getEncoder().encode(secretKey.getEncoded()), DEFAULT_ENCODE);
return Optional.of(keys);
} catch (IOException | GeneralSecurityException e) {
return Optional.empty();
}
}

/**
* encrypt
*
* @param key key
* @param text text
* @return encrypted text
*/
public static Optional<String> encrypt(String key, String text) {
try {
Cipher cipher = Cipher.getInstance(AES_PADDING);
byte[] keyBytes = Base64.getDecoder().decode(key.getBytes(DEFAULT_ENCODE));
SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptBytes = cipher.doFinal(text.getBytes(DEFAULT_ENCODE));
byte[] bytes = new byte[IV_LENGTH + text.getBytes(DEFAULT_ENCODE).length + LENGTH];
System.arraycopy(cipher.getIV(), 0, bytes, 0, IV_LENGTH);
System.arraycopy(encryptBytes, 0, bytes, IV_LENGTH, encryptBytes.length);
return Optional.of(new String(Base64.getEncoder().encode(bytes), DEFAULT_ENCODE));
} catch (IOException | GeneralSecurityException e) {
return Optional.empty();
}
}
}
Loading

0 comments on commit ccdc520

Please sign in to comment.