从AWS SSM检索Dropwizard

时间:2018-03-08 20:34:15

标签: amazon-web-services dropwizard aws-ssm

我是Dropwizard的新手,并希望在应用程序引导时从AWS SSM参数存储中检索机密。关于如何做到这一点我有两个问题:

  1. 我应该在哪里这样做?在initialize方法中?
  2. 一旦我调用AWS SSM参数存储,哪里是放置它的最佳位置?我快速浏览了一下Bootstrap类,但是对于我来说,我应该把秘密之类的内容放在哪里以便以后检索。
  3. 由于

1 个答案:

答案 0 :(得分:0)

您可以通过设置public class FamilyTreeApp extends JFrame implements ActionListener{ private JLabel Header; private JPanel Body; private JScrollPane FamilyTree; private JButton createTreeB, saveTreeB, loadTreeB, editTreeB; //skipped gui components Container pane = getContentPane(); pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = 4; c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.FIRST_LINE_START; pane.add(Header, c); c.gridwidth = 1; c.gridx = 0; c.gridy = 1; pane.add(createTreeB, c); c.gridx = 1; c.gridy = 1; pane.add(loadTreeB, c); c.gridx = 2; c.gridy = 1; pane.add(saveTreeB, c); c.gridwidth = 2; c.gridx = 0; c.gridy = 2; c.insets = new Insets(15,0,0,0); FamilyTree.setPreferredSize(new Dimension(250,600)); pane.add(FamilyTree, c); c.gridwidth = 2; c.gridx = 2; c.gridy = 2; Body.setPreferredSize(new Dimension(250,600)); pane.add(Body, c); public void actionPerformed(ActionEvent e) { if(e.getSource() == createTreeB) { getContentPane().removeAll(); TC.preparePanel(); PrepareGUI(); repaint(); revalidate(); } public class TreeCreation extends JFrame implements ActionListener { private JPanel mainPanel; private JScrollPane mainScrollPane; private JLabel LOwner, LFather, LMother, LSpouse, LfirstChild, LsecondChild, fName, bSurname, mSurname, gender, streetNumber, streetName, suburb, postCode, description; private JTextField Name, faName, mName, sName, c1Name, c2Name; private JTextField originSurname, faOriginSurname, mOriginSurname, sOriginSurname, c1OriginSurname, c2OriginSurname; private JTextField marriageSurname, faMarriageSurname, mMarriageSurname, sMarriageSurname, c1MarriageSurname, c2MarriageSurname; private JTextField Gender, faGender, mGender, sGender, c1Gender, c2Gender; private JTextField stNumber, faStNumber, mStNumber, sStNumber, c1StNumber, c2StNumber; private JTextField stName, faStName, mStName, sStName, c1StName, c2StName; private JTextField Suburb, faSuburb, mSuburb, sSuburb, c1Suburb, c2Suburb; private JTextField postalCode, faPostalCode, mPostalCode, sPostalCode, c1PostalCode, c2PostalCode; private JTextArea Description, faDescription, mDescription, sDescription, c1Description, c2Description; private JButton submitButton, clearButton; public void preparePanel() { Container pane = getContentPane(); pane.setLayout(new BorderLayout()); mainPanel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; //skipped inits fields, label and adding them to mainPanel public void actionPerformed(ActionEvent e) { if(e.getSource() == submitButton) { Person Owner = new Person(); Person Father = new Person(); Person Mother = new Person(); Person Spouse = new Person(); Person Child1 = new Person(); Person Child2 = new Person(); Owner.setName(Name.getText()); Owner.setbSurname(originSurname.getText()); Owner.setmSurname(marriageSurname.getText()); Owner.setGender(Gender.getText()); Owner.setStreetNumber(stNumber.getText()); Owner.setStreetName(stName.getText()); Owner.setSuburb(Suburb.getText()); Owner.setPostcode(postalCode.getText()); Owner.setDescription(Description.getText()); Father.setName(faName.getText()); Father.setbSurname(faOriginSurname.getText()); Father.setmSurname(faMarriageSurname.getText()); Father.setGender(faGender.getText()); Father.setStreetNumber(faStNumber.getText()); Father.setStreetName(faStName.getText()); Father.setSuburb(faSuburb.getText()); Father.setPostcode(faPostalCode.getText()); Father.setDescription(faDescription.getText()); Mother.setName(mName.getText()); Mother.setbSurname(mOriginSurname.getText()); Mother.setmSurname(mMarriageSurname.getText()); Mother.setGender(mGender.getText()); Mother.setStreetNumber(mStNumber.getText()); Mother.setStreetName(mStName.getText()); Mother.setSuburb(mSuburb.getText()); Mother.setPostcode(mPostalCode.getText()); Mother.setDescription(mDescription.getText()); Spouse.setName(sName.getText()); Spouse.setbSurname(sOriginSurname.getText()); Spouse.setmSurname(sMarriageSurname.getText()); Spouse.setGender(sGender.getText()); Spouse.setStreetNumber(sStNumber.getText()); Spouse.setStreetName(sStName.getText()); Spouse.setSuburb(sSuburb.getText()); Spouse.setPostcode(sPostalCode.getText()); Spouse.setDescription(sDescription.getText()); Child1.setName(c1Name.getText()); Child1.setbSurname(c1OriginSurname.getText()); Child1.setmSurname(c1MarriageSurname.getText()); Child1.setGender(c1Gender.getText()); Child1.setStreetNumber(c1StNumber.getText()); Child1.setStreetName(c1StName.getText()); Child1.setSuburb(c1Suburb.getText()); Child1.setPostcode(c1PostalCode.getText()); Child1.setDescription(c1Description.getText()); Child2.setName(c2Name.getText()); Child2.setbSurname(c2OriginSurname.getText()); Child2.setmSurname(c2MarriageSurname.getText()); Child2.setGender(c2Gender.getText()); Child2.setStreetNumber(c2StNumber.getText()); Child2.setStreetName(c2StName.getText()); Child2.setSuburb(c2Suburb.getText()); Child2.setPostcode(c2PostalCode.getText()); Child2.setDescription(c2Description.getText()); FamilyTree FT = new FamilyTree(); FT.CreateTree(Owner, Father, Mother, Spouse, Child1, Child2); public class FamilyTree { private JTree t1; /* Person Owner = new Person("John", "Tan", "Tan", "Male", "10", "Jurong East", "Jurong", "600111", "I'm John"); Person Father = new Person("Johnny", "Tan", "Tan", "Male", "20", "Jurong West", "Jurong", "600222", "I'm John's Father"); Person Mother = new Person("Alice", "Ko", "Tan", "Female", "20", "Jurong West", "Jurong", "600222", "I'm John's Mother"); Person Spouse = new Person("Angela", "Lim", "Tan", "Female", "10", "Jurong East", "Jurong", "600111", "I'm John's Wife"); Person Child1 = new Person("Brandon", "Tan", "Nil", "Male", "10", "Jurong East", "Jurong", "600111", "I'm John's Son"); Person Child2 = new Person("Wendy", "Tan", "Nil", "Female", "10", "Jurong East", "Jurong", "600111", "I'm John's Daughter"); */ Vector<Person> Parents; Vector<Person> Childrens; Vector<Person> Spouses; public void CreateTree() { same as below } public void CreateTree(Person Owner, Person Father, Person Mother, Person Spouse, Person Child1, Person Child2) { Parents = new Vector(); Parents.add(Father); Parents.add(Mother); Spouses = new Vector(); Spouses.add(Spouse); Childrens = new Vector(); Childrens.add(Child1); Childrens.add(Child2); Hashtable<String, Object> ht = new Hashtable<String, Object>(); ht.put("Parents", Parents); ht.put("Spouse", Spouses); ht.put("Childrens", Childrens); DefaultMutableTreeNode root = new DefaultMutableTreeNode(Owner); JTree.DynamicUtilTreeNode.createChildren(root, ht); t1 = new JTree(root); } 来实现所需的目标,在该ConfigurationSourceProvider中用参数存储变量的值替换配置设置。

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    ParameterStoreSourceProvider parameterStoreSourceProvider =
            new ParameterStoreSourceProvider(bootstrap.getConfigurationSourceProvider());
    bootstrap.setConfigurationSourceProvider(parameterStoreSourceProvider);
}

实施ConfigurationSourceProvider,该配置从委托手中接管配置输入流以替换值。

public class ParameterStoreSourceProvider implements ConfigurationSourceProvider {
    private final ConfigurationSourceProvider delegate;
    private final StringSubstitutor substitutor;

    public ParameterStoreSourceProvider(ConfigurationSourceProvider delegate) {
        this.delegate = delegate;
        this.substitutor = new StringSubstitutor(new ParameterStoreLookup());
    }

    @Override
    public InputStream open(String path) throws IOException {
        try (Scanner scanner = new Scanner(delegate.open(path))) {
            String config = scanner.useDelimiter("\\A").next();
            String substituted = substitutor.replace(config);
            return new ByteArrayInputStream(substituted.getBytes(StandardCharsets.UTF_8));
        }
    }
}

ParameterStoreLookup所在的StringSubstitutor处将其用作变量解析器。

public class ParameterStoreLookup implements StringLookup {
    @Override
    public String lookup(String s) {
        // Lookup the value from Parameter Store and return.
        return null;
    }
}

完成上述步骤后,您可以在配置文件中定义一个变量,如下所示:

database:
    driverClass : org.postgresql.Driver
    url: ${DB_URL}
    user: ${DB_USER}
    password: ${DB_PASSWORD}
logging:
    level: INFO
    appenders:
        - type: console

有关定义变量的更多信息,请参见StringSubstitutor。确保将commons-text添加到您的依赖项中。