init.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 Yunion
  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. package saml
  15. import (
  16. "os"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/samlutils"
  19. "yunion.io/x/onecloud/pkg/httperrors"
  20. "yunion.io/x/onecloud/pkg/keystone/options"
  21. "yunion.io/x/onecloud/pkg/util/seclib2"
  22. )
  23. var (
  24. saml *samlutils.SSAMLInstance
  25. )
  26. func InitSAMLInstance() error {
  27. certfile := options.Options.SslCertfile
  28. if len(options.Options.SslCaCerts) > 0 {
  29. var err error
  30. certfile, err = seclib2.MergeCaCertFiles(options.Options.SslCaCerts, options.Options.SslCertfile)
  31. if err != nil {
  32. return errors.Wrapf(httperrors.ErrInputParameter, "fail to merge ca+cert content: %s", err)
  33. }
  34. defer os.Remove(certfile)
  35. }
  36. if len(certfile) == 0 {
  37. return errors.Wrap(httperrors.ErrInputParameter, "Missing ssl-certfile")
  38. }
  39. if len(options.Options.SslKeyfile) == 0 {
  40. return errors.Wrap(httperrors.ErrInputParameter, "Missing ssl-keyfile")
  41. }
  42. var err error
  43. saml, err = samlutils.NewSAMLInstance(options.Options.ApiServer, certfile, options.Options.SslKeyfile)
  44. if err != nil {
  45. return errors.Wrap(err, "samlutils.NewSAMLInstance")
  46. }
  47. return nil
  48. }
  49. func SAMLInstance() *samlutils.SSAMLInstance {
  50. if saml.GetEntityId() != options.Options.ApiServer {
  51. saml.SetEntityId(options.Options.ApiServer)
  52. }
  53. return saml
  54. }
  55. func IsSAMLEnabled() bool {
  56. return saml != nil
  57. }