acme_dns.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package acme
  2. import (
  3. "errors"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/go-acme/lego/v4/challenge"
  8. "imuslab.com/zoraxy/mod/acme/acmedns"
  9. )
  10. func GetDnsChallengeProviderByName(dnsProvider string, dnsCredentials string) (challenge.Provider, error) {
  11. //Original Implementation
  12. /*credentials, err := extractDnsCredentials(dnsCredentials)
  13. if err != nil {
  14. return nil, err
  15. }
  16. setCredentialsIntoEnvironmentVariables(credentials)
  17. provider, err := dns.NewDNSChallengeProviderByName(dnsProvider)
  18. */
  19. //New implementation using acmedns CICD pipeline generated datatype
  20. return acmedns.GetDNSProviderByJsonConfig(dnsProvider, dnsCredentials)
  21. }
  22. /*
  23. Original implementation of DNS ACME using OS.Env as payload
  24. */
  25. func setCredentialsIntoEnvironmentVariables(credentials map[string]string) {
  26. for key, value := range credentials {
  27. err := os.Setenv(key, value)
  28. if err != nil {
  29. log.Println("[ERR] Failed to set environment variable %s: %v", key, err)
  30. } else {
  31. log.Println("[INFO] Environment variable %s set successfully", key)
  32. }
  33. }
  34. }
  35. func extractDnsCredentials(input string) (map[string]string, error) {
  36. result := make(map[string]string)
  37. // Split the input string by newline character
  38. lines := strings.Split(input, "\n")
  39. // Iterate over each line
  40. for _, line := range lines {
  41. // Split the line by "=" character
  42. //use SpliyN to make sure not to split the value if the value is base64
  43. parts := strings.SplitN(line, "=", 1)
  44. // Check if the line is in the correct format
  45. if len(parts) == 2 {
  46. key := strings.TrimSpace(parts[0])
  47. value := strings.TrimSpace(parts[1])
  48. // Add the key-value pair to the map
  49. result[key] = value
  50. if value == "" || key == "" {
  51. //invalid config
  52. return result, errors.New("DNS credential extract failed")
  53. }
  54. }
  55. }
  56. return result, nil
  57. }