Charm Interface - Barbican HSM plugin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.5KB

  1. # Copyright 2016 Canonical Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. import charms.reactive as reactive
  16. class BarbicanProvides(reactive.RelationBase):
  17. """This is the barbican-{type}hsm end of the relation
  18. The HSM provider needs to set it's name (which may be relevant) and
  19. also provide any plugin data to Barbican.
  20. The plugin data is:
  21. {
  22. "library_path": <library path of PKCS#11 libary>,
  23. "login": the pin/password for accessing the PKCS#11 token,
  24. "slot_id": <int: slot_id of the token for barbican to use>
  25. }
  26. """
  27. scope = reactive.scopes.GLOBAL
  28. # These remote data fields will be automatically mapped to accessors
  29. # with a basic documentation string provided.
  30. auto_accessors = []
  31. @reactive.hook('{provides:barbican-hsm}-relation-joined')
  32. def joined(self):
  33. self.set_state('{relation_name}.connected')
  34. self.set_state('{relation_name}.available')
  35. @reactive.hook('{provides:barbican-hsm}-relation-changed')
  36. def changed(self):
  37. pass
  38. @reactive.hook('{provides:barbican-hsm}-relation-{broken,departed}')
  39. def departed(self):
  40. self.remove_state('{relation_name}.available')
  41. self.remove_state('{relation_name}.connected')
  42. def set_name(self, name):
  43. """Set the name of the plugin.
  44. :param name: string
  45. """
  46. self.set_remote(_name=name)
  47. def set_plugin_data(self, data):
  48. """Set the plugin data for the remote (Barbican charm) to be able to
  49. configure barbican to use the HSM that is installed alongside it.
  50. NOTE that the data is wrapped in a dictionary, converted to JSON and
  51. then placed in the juju remote variable. The other 'end' unpacks this
  52. and provides the original data to Barbican charm.
  53. Thus data has to be JSONable.
  54. :param data: object that describes the plugin data to be sent.
  55. """
  56. self.set_remote(_plugin_data=json.dumps({"data": data}))