airflow_metadata.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from typing import Any
  17. from elyra.metadata.manager import MetadataManager
  18. from elyra.pipeline.runtimes_metadata import RuntimesMetadata
  19. from elyra.util.gitutil import SupportedGitTypes
  20. class AirflowMetadata(RuntimesMetadata):
  21. """
  22. Applies changes specific to the Airflow schema
  23. """
  24. def on_load(self, **kwargs: Any) -> None:
  25. super().on_load(**kwargs)
  26. update_required = False
  27. if self.metadata.get("git_type") is None:
  28. # Inject git_type property for metadata persisted using Elyra < 3.5:
  29. self.metadata["git_type"] = SupportedGitTypes.GITHUB.name
  30. update_required = True
  31. if self.metadata.get("cos_auth_type") is None:
  32. # Inject cos_auth_type property for metadata persisted using Elyra < 3.4:
  33. # - cos_username and cos_password must be present
  34. # - cos_secret may be present (above statement also applies in this case)
  35. if self.metadata.get("cos_username") and self.metadata.get("cos_password"):
  36. if len(self.metadata.get("cos_secret", "")) == 0:
  37. self.metadata["cos_auth_type"] = "USER_CREDENTIALS"
  38. else:
  39. self.metadata["cos_auth_type"] = "KUBERNETES_SECRET"
  40. update_required = True
  41. if update_required:
  42. # save changes
  43. MetadataManager(schemaspace="runtimes").update(self.name, self, for_migration=True)
  44. return None
  45. def pre_save(self, **kwargs: Any) -> None:
  46. """
  47. This method enforces conditional constraints related to
  48. COS authentication type properties.
  49. TODO: Remove after https://github.com/elyra-ai/elyra/issues/2338 was resolved.
  50. """
  51. super().pre_save(**kwargs)
  52. if self.metadata.get("cos_auth_type") is None:
  53. # nothing to do
  54. return
  55. if self.metadata["cos_auth_type"] == "USER_CREDENTIALS":
  56. if (
  57. len(self.metadata.get("cos_username", "").strip()) == 0
  58. or len(self.metadata.get("cos_password", "").strip()) == 0
  59. ):
  60. raise ValueError(
  61. "A username and password are required " "for the selected Object Storage authentication type."
  62. )
  63. if len(self.metadata.get("cos_secret", "").strip()) > 0:
  64. raise ValueError(
  65. "Kubernetes secrets are not supported " "for the selected Object Storage authentication type."
  66. )
  67. elif self.metadata["cos_auth_type"] == "KUBERNETES_SECRET":
  68. if (
  69. len(self.metadata.get("cos_username", "").strip()) == 0
  70. or len(self.metadata.get("cos_password", "").strip()) == 0
  71. or len(self.metadata.get("cos_secret", "").strip()) == 0
  72. ):
  73. raise ValueError(
  74. "Username, password, and Kubernetes secret are required "
  75. "for the selected Object Storage authentication type."
  76. )
  77. elif self.metadata["cos_auth_type"] == "AWS_IAM_ROLES_FOR_SERVICE_ACCOUNTS":
  78. if (
  79. len(self.metadata.get("cos_username", "").strip()) > 0
  80. or len(self.metadata.get("cos_password", "").strip()) > 0
  81. or len(self.metadata.get("cos_secret", "").strip()) > 0
  82. ):
  83. raise ValueError(
  84. "Username, password, and Kubernetes secret are not supported "
  85. "for the selected Object Storage authentication type."
  86. )