首页 > 解决方案 > 配置 bitbucket 插件以避免对安全变量进行硬编码

问题描述

我开发了一个 Atlasian Bitbucket 插件,它可以全局监听推送/PR,并使用 REST API 将存储库详细信息发送到数据库。

我需要进行配置REST API URLcredential以便我的插件可以进行 API 调用。目前我有hardcoded REST API URL并且credential在我的插件属性文件中。我不喜欢,因为每次如果我需要创建一个包来针对我的测试环境或生产环境,我都必须进行更改。另外,我不喜欢在源代码中保留凭据。

在 bitbucket 插件中添加配置屏幕的最佳方法是什么?我想要 URL、用户名和密码的表单(一旦我安装了插件)并且只更新一次 Bitbucket 中的存储。如果我需要重新启动我的 bitbucket,我不想丢失保存的数据。

我试图搜索如何配置 bitbucket 插件,但是找不到简单的方法。我确实看到了多种方法,例如添加“配置”按钮,该按钮将打开一个 serverlet 以获取用户输入。对我来说似乎很神秘。另外,我看到很多关于模板的建议,例如速度、大豆等,这让我很困惑。

由于我是插件开发的新手,因此无法探索。寻求帮助。

标签: bitbucketbitbucket-apiatlassian-plugin-sdk

解决方案


简单配置文件的一种可能性是从 Bitbucket 主目录读取 somefile.properties,这样配置文件将在应用程序更新后继续存在。

  1. 创建somefile.propertiesBITBUCKET_HOME
server.username=YOUR_USERNAME
server.password=YOUR_PASSWORD
  1. 像这样读取插件类中的属性
// imports
import com.atlassian.bitbucket.server.StorageService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

private final StorageService storageService;

// StorageService injected via constructor injection
public SomePlugin(@ComponentImport final StorageService storageService) {
        this.storageService = storageService;
}

Properties p = new Properties();

File file = new File(storageService.getHomeDir().toString(), "somefile.properties");
FileInputStream fileInputStream;
try {
    fileInputStream = new FileInputStream(file);
    p.load(fileInputStream);

    String username = p.getProperty("server.username");
    String password = p.getProperty("server.password");

} catch (IOException e) {
    //handle exception
}

推荐阅读