cluster.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package controlapi
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/docker/swarmkit/api"
  6. "github.com/docker/swarmkit/ca"
  7. "github.com/docker/swarmkit/log"
  8. "github.com/docker/swarmkit/manager/encryption"
  9. "github.com/docker/swarmkit/manager/state/store"
  10. gogotypes "github.com/gogo/protobuf/types"
  11. "golang.org/x/net/context"
  12. "google.golang.org/grpc"
  13. "google.golang.org/grpc/codes"
  14. )
  15. const (
  16. // expiredCertGrace is the amount of time to keep a node in the
  17. // blacklist beyond its certificate expiration timestamp.
  18. expiredCertGrace = 24 * time.Hour * 7
  19. )
  20. func validateClusterSpec(spec *api.ClusterSpec) error {
  21. if spec == nil {
  22. return grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
  23. }
  24. // Validate that expiry time being provided is valid, and over our minimum
  25. if spec.CAConfig.NodeCertExpiry != nil {
  26. expiry, err := gogotypes.DurationFromProto(spec.CAConfig.NodeCertExpiry)
  27. if err != nil {
  28. return grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
  29. }
  30. if expiry < ca.MinNodeCertExpiration {
  31. return grpc.Errorf(codes.InvalidArgument, "minimum certificate expiry time is: %s", ca.MinNodeCertExpiration)
  32. }
  33. }
  34. // Validate that AcceptancePolicies only include Secrets that are bcrypted
  35. // TODO(diogo): Add a global list of acceptance algorithms. We only support bcrypt for now.
  36. if len(spec.AcceptancePolicy.Policies) > 0 {
  37. for _, policy := range spec.AcceptancePolicy.Policies {
  38. if policy.Secret != nil && strings.ToLower(policy.Secret.Alg) != "bcrypt" {
  39. return grpc.Errorf(codes.InvalidArgument, "hashing algorithm is not supported: %s", policy.Secret.Alg)
  40. }
  41. }
  42. }
  43. // Validate that heartbeatPeriod time being provided is valid
  44. if spec.Dispatcher.HeartbeatPeriod != nil {
  45. heartbeatPeriod, err := gogotypes.DurationFromProto(spec.Dispatcher.HeartbeatPeriod)
  46. if err != nil {
  47. return grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
  48. }
  49. if heartbeatPeriod < 0 {
  50. return grpc.Errorf(codes.InvalidArgument, "heartbeat time period cannot be a negative duration")
  51. }
  52. }
  53. return nil
  54. }
  55. // GetCluster returns a Cluster given a ClusterID.
  56. // - Returns `InvalidArgument` if ClusterID is not provided.
  57. // - Returns `NotFound` if the Cluster is not found.
  58. func (s *Server) GetCluster(ctx context.Context, request *api.GetClusterRequest) (*api.GetClusterResponse, error) {
  59. if request.ClusterID == "" {
  60. return nil, grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
  61. }
  62. var cluster *api.Cluster
  63. s.store.View(func(tx store.ReadTx) {
  64. cluster = store.GetCluster(tx, request.ClusterID)
  65. })
  66. if cluster == nil {
  67. return nil, grpc.Errorf(codes.NotFound, "cluster %s not found", request.ClusterID)
  68. }
  69. redactedClusters := redactClusters([]*api.Cluster{cluster})
  70. // WARN: we should never return cluster here. We need to redact the private fields first.
  71. return &api.GetClusterResponse{
  72. Cluster: redactedClusters[0],
  73. }, nil
  74. }
  75. // UpdateCluster updates a Cluster referenced by ClusterID with the given ClusterSpec.
  76. // - Returns `NotFound` if the Cluster is not found.
  77. // - Returns `InvalidArgument` if the ClusterSpec is malformed.
  78. // - Returns `Unimplemented` if the ClusterSpec references unimplemented features.
  79. // - Returns an error if the update fails.
  80. func (s *Server) UpdateCluster(ctx context.Context, request *api.UpdateClusterRequest) (*api.UpdateClusterResponse, error) {
  81. if request.ClusterID == "" || request.ClusterVersion == nil {
  82. return nil, grpc.Errorf(codes.InvalidArgument, errInvalidArgument.Error())
  83. }
  84. if err := validateClusterSpec(request.Spec); err != nil {
  85. return nil, err
  86. }
  87. var cluster *api.Cluster
  88. err := s.store.Update(func(tx store.Tx) error {
  89. cluster = store.GetCluster(tx, request.ClusterID)
  90. if cluster == nil {
  91. return grpc.Errorf(codes.NotFound, "cluster %s not found", request.ClusterID)
  92. }
  93. // This ensures that we have the current rootCA with which to generate tokens (expiration doesn't matter
  94. // for generating the tokens)
  95. rootCA, err := ca.RootCAFromAPI(ctx, &cluster.RootCA, ca.DefaultNodeCertExpiration)
  96. if err != nil {
  97. log.G(ctx).WithField(
  98. "method", "(*controlapi.Server).UpdateCluster").WithError(err).Error("invalid cluster root CA")
  99. return grpc.Errorf(codes.Internal, "error loading cluster rootCA for update")
  100. }
  101. cluster.Meta.Version = *request.ClusterVersion
  102. cluster.Spec = *request.Spec.Copy()
  103. expireBlacklistedCerts(cluster)
  104. if request.Rotation.WorkerJoinToken {
  105. cluster.RootCA.JoinTokens.Worker = ca.GenerateJoinToken(&rootCA)
  106. }
  107. if request.Rotation.ManagerJoinToken {
  108. cluster.RootCA.JoinTokens.Manager = ca.GenerateJoinToken(&rootCA)
  109. }
  110. updatedRootCA, err := validateCAConfig(ctx, s.securityConfig, cluster)
  111. if err != nil {
  112. return err
  113. }
  114. cluster.RootCA = *updatedRootCA
  115. var unlockKeys []*api.EncryptionKey
  116. var managerKey *api.EncryptionKey
  117. for _, eKey := range cluster.UnlockKeys {
  118. if eKey.Subsystem == ca.ManagerRole {
  119. if !cluster.Spec.EncryptionConfig.AutoLockManagers {
  120. continue
  121. }
  122. managerKey = eKey
  123. }
  124. unlockKeys = append(unlockKeys, eKey)
  125. }
  126. switch {
  127. case !cluster.Spec.EncryptionConfig.AutoLockManagers:
  128. break
  129. case managerKey == nil:
  130. unlockKeys = append(unlockKeys, &api.EncryptionKey{
  131. Subsystem: ca.ManagerRole,
  132. Key: encryption.GenerateSecretKey(),
  133. })
  134. case request.Rotation.ManagerUnlockKey:
  135. managerKey.Key = encryption.GenerateSecretKey()
  136. }
  137. cluster.UnlockKeys = unlockKeys
  138. return store.UpdateCluster(tx, cluster)
  139. })
  140. if err != nil {
  141. return nil, err
  142. }
  143. redactedClusters := redactClusters([]*api.Cluster{cluster})
  144. // WARN: we should never return cluster here. We need to redact the private fields first.
  145. return &api.UpdateClusterResponse{
  146. Cluster: redactedClusters[0],
  147. }, nil
  148. }
  149. func filterClusters(candidates []*api.Cluster, filters ...func(*api.Cluster) bool) []*api.Cluster {
  150. result := []*api.Cluster{}
  151. for _, c := range candidates {
  152. match := true
  153. for _, f := range filters {
  154. if !f(c) {
  155. match = false
  156. break
  157. }
  158. }
  159. if match {
  160. result = append(result, c)
  161. }
  162. }
  163. return result
  164. }
  165. // ListClusters returns a list of all clusters.
  166. func (s *Server) ListClusters(ctx context.Context, request *api.ListClustersRequest) (*api.ListClustersResponse, error) {
  167. var (
  168. clusters []*api.Cluster
  169. err error
  170. )
  171. s.store.View(func(tx store.ReadTx) {
  172. switch {
  173. case request.Filters != nil && len(request.Filters.Names) > 0:
  174. clusters, err = store.FindClusters(tx, buildFilters(store.ByName, request.Filters.Names))
  175. case request.Filters != nil && len(request.Filters.NamePrefixes) > 0:
  176. clusters, err = store.FindClusters(tx, buildFilters(store.ByNamePrefix, request.Filters.NamePrefixes))
  177. case request.Filters != nil && len(request.Filters.IDPrefixes) > 0:
  178. clusters, err = store.FindClusters(tx, buildFilters(store.ByIDPrefix, request.Filters.IDPrefixes))
  179. default:
  180. clusters, err = store.FindClusters(tx, store.All)
  181. }
  182. })
  183. if err != nil {
  184. return nil, err
  185. }
  186. if request.Filters != nil {
  187. clusters = filterClusters(clusters,
  188. func(e *api.Cluster) bool {
  189. return filterContains(e.Spec.Annotations.Name, request.Filters.Names)
  190. },
  191. func(e *api.Cluster) bool {
  192. return filterContainsPrefix(e.Spec.Annotations.Name, request.Filters.NamePrefixes)
  193. },
  194. func(e *api.Cluster) bool {
  195. return filterContainsPrefix(e.ID, request.Filters.IDPrefixes)
  196. },
  197. func(e *api.Cluster) bool {
  198. return filterMatchLabels(e.Spec.Annotations.Labels, request.Filters.Labels)
  199. },
  200. )
  201. }
  202. // WARN: we should never return cluster here. We need to redact the private fields first.
  203. return &api.ListClustersResponse{
  204. Clusters: redactClusters(clusters),
  205. }, nil
  206. }
  207. // redactClusters is a method that enforces a whitelist of fields that are ok to be
  208. // returned in the Cluster object. It should filter out all sensitive information.
  209. func redactClusters(clusters []*api.Cluster) []*api.Cluster {
  210. var redactedClusters []*api.Cluster
  211. // Only add public fields to the new clusters
  212. for _, cluster := range clusters {
  213. // Copy all the mandatory fields
  214. // Do not copy secret keys
  215. redactedSpec := cluster.Spec.Copy()
  216. redactedSpec.CAConfig.SigningCAKey = nil
  217. // the cert is not a secret, but if API users get the cluster spec and then update,
  218. // then because the cert is included but not the key, the user can get update errors
  219. // or unintended consequences (such as telling swarm to forget about the key so long
  220. // as there is a corresponding external CA)
  221. redactedSpec.CAConfig.SigningCACert = nil
  222. redactedRootCA := cluster.RootCA.Copy()
  223. redactedRootCA.CAKey = nil
  224. if r := redactedRootCA.RootRotation; r != nil {
  225. r.CAKey = nil
  226. }
  227. newCluster := &api.Cluster{
  228. ID: cluster.ID,
  229. Meta: cluster.Meta,
  230. Spec: *redactedSpec,
  231. RootCA: *redactedRootCA,
  232. BlacklistedCertificates: cluster.BlacklistedCertificates,
  233. }
  234. redactedClusters = append(redactedClusters, newCluster)
  235. }
  236. return redactedClusters
  237. }
  238. func expireBlacklistedCerts(cluster *api.Cluster) {
  239. nowMinusGrace := time.Now().Add(-expiredCertGrace)
  240. for cn, blacklistedCert := range cluster.BlacklistedCertificates {
  241. if blacklistedCert.Expiry == nil {
  242. continue
  243. }
  244. expiry, err := gogotypes.TimestampFromProto(blacklistedCert.Expiry)
  245. if err == nil && nowMinusGrace.After(expiry) {
  246. delete(cluster.BlacklistedCertificates, cn)
  247. }
  248. }
  249. }