common.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package controlapi
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/docker/docker/pkg/plugingetter"
  6. "github.com/docker/libnetwork/driverapi"
  7. "github.com/docker/libnetwork/ipamapi"
  8. "github.com/docker/swarmkit/api"
  9. "github.com/docker/swarmkit/manager/allocator"
  10. "github.com/docker/swarmkit/manager/state/store"
  11. "google.golang.org/grpc/codes"
  12. "google.golang.org/grpc/status"
  13. )
  14. var isValidDNSName = regexp.MustCompile(`^[a-zA-Z0-9](?:[-_]*[A-Za-z0-9]+)*$`)
  15. // configs and secrets have different naming requirements from tasks and services
  16. var isValidConfigOrSecretName = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[a-zA-Z0-9-_.]*[a-zA-Z0-9])?$`)
  17. func buildFilters(by func(string) store.By, values []string) store.By {
  18. filters := make([]store.By, 0, len(values))
  19. for _, v := range values {
  20. filters = append(filters, by(v))
  21. }
  22. return store.Or(filters...)
  23. }
  24. func filterContains(match string, candidates []string) bool {
  25. if len(candidates) == 0 {
  26. return true
  27. }
  28. for _, c := range candidates {
  29. if c == match {
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. func filterContainsPrefix(match string, candidates []string) bool {
  36. if len(candidates) == 0 {
  37. return true
  38. }
  39. for _, c := range candidates {
  40. if strings.HasPrefix(match, c) {
  41. return true
  42. }
  43. }
  44. return false
  45. }
  46. func filterMatchLabels(match map[string]string, candidates map[string]string) bool {
  47. if len(candidates) == 0 {
  48. return true
  49. }
  50. for k, v := range candidates {
  51. c, ok := match[k]
  52. if !ok {
  53. return false
  54. }
  55. if v != "" && v != c {
  56. return false
  57. }
  58. }
  59. return true
  60. }
  61. func validateAnnotations(m api.Annotations) error {
  62. if m.Name == "" {
  63. return status.Errorf(codes.InvalidArgument, "meta: name must be provided")
  64. }
  65. if !isValidDNSName.MatchString(m.Name) {
  66. // if the name doesn't match the regex
  67. return status.Errorf(codes.InvalidArgument, "name must be valid as a DNS name component")
  68. }
  69. if len(m.Name) > 63 {
  70. // DNS labels are limited to 63 characters
  71. return status.Errorf(codes.InvalidArgument, "name must be 63 characters or fewer")
  72. }
  73. return nil
  74. }
  75. func validateConfigOrSecretAnnotations(m api.Annotations) error {
  76. if m.Name == "" {
  77. return status.Errorf(codes.InvalidArgument, "name must be provided")
  78. } else if len(m.Name) > 64 || !isValidConfigOrSecretName.MatchString(m.Name) {
  79. // if the name doesn't match the regex
  80. return status.Errorf(codes.InvalidArgument,
  81. "invalid name, only 64 [a-zA-Z0-9-_.] characters allowed, and the start and end character must be [a-zA-Z0-9]")
  82. }
  83. return nil
  84. }
  85. func validateDriver(driver *api.Driver, pg plugingetter.PluginGetter, pluginType string) error {
  86. if driver == nil {
  87. // It is ok to not specify the driver. We will choose
  88. // a default driver.
  89. return nil
  90. }
  91. if driver.Name == "" {
  92. return status.Errorf(codes.InvalidArgument, "driver name: if driver is specified name is required")
  93. }
  94. // First check against the known drivers
  95. switch pluginType {
  96. case ipamapi.PluginEndpointType:
  97. if strings.ToLower(driver.Name) == ipamapi.DefaultIPAM {
  98. return nil
  99. }
  100. case driverapi.NetworkPluginEndpointType:
  101. if allocator.IsBuiltInNetworkDriver(driver.Name) {
  102. return nil
  103. }
  104. default:
  105. }
  106. if pg == nil {
  107. return status.Errorf(codes.InvalidArgument, "plugin %s not supported", driver.Name)
  108. }
  109. p, err := pg.Get(driver.Name, pluginType, plugingetter.Lookup)
  110. if err != nil {
  111. return status.Errorf(codes.InvalidArgument, "error during lookup of plugin %s", driver.Name)
  112. }
  113. if p.IsV1() {
  114. return status.Errorf(codes.InvalidArgument, "legacy plugin %s of type %s is not supported in swarm mode", driver.Name, pluginType)
  115. }
  116. return nil
  117. }