github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/notify/legacy.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package notify
    19  
    20  import (
    21  	"fmt"
    22  	"strconv"
    23  	"strings"
    24  
    25  	"github.com/minio/minio/internal/config"
    26  	"github.com/minio/minio/internal/event/target"
    27  )
    28  
    29  // SetNotifyKafka - helper for config migration from older config.
    30  func SetNotifyKafka(s config.Config, name string, cfg target.KafkaArgs) error {
    31  	if !cfg.Enable {
    32  		return nil
    33  	}
    34  
    35  	if err := cfg.Validate(); err != nil {
    36  		return err
    37  	}
    38  
    39  	s[config.NotifyKafkaSubSys][name] = config.KVS{
    40  		config.KV{
    41  			Key:   config.Enable,
    42  			Value: config.EnableOn,
    43  		},
    44  		config.KV{
    45  			Key: target.KafkaBrokers,
    46  			Value: func() string {
    47  				var brokers []string
    48  				for _, broker := range cfg.Brokers {
    49  					brokers = append(brokers, broker.String())
    50  				}
    51  				return strings.Join(brokers, config.ValueSeparator)
    52  			}(),
    53  		},
    54  		config.KV{
    55  			Key:   target.KafkaTopic,
    56  			Value: cfg.Topic,
    57  		},
    58  		config.KV{
    59  			Key:   target.KafkaQueueDir,
    60  			Value: cfg.QueueDir,
    61  		},
    62  		config.KV{
    63  			Key:   target.KafkaClientTLSCert,
    64  			Value: cfg.TLS.ClientTLSCert,
    65  		},
    66  		config.KV{
    67  			Key:   target.KafkaClientTLSKey,
    68  			Value: cfg.TLS.ClientTLSKey,
    69  		},
    70  		config.KV{
    71  			Key:   target.KafkaQueueLimit,
    72  			Value: strconv.Itoa(int(cfg.QueueLimit)),
    73  		},
    74  		config.KV{
    75  			Key:   target.KafkaTLS,
    76  			Value: config.FormatBool(cfg.TLS.Enable),
    77  		},
    78  		config.KV{
    79  			Key:   target.KafkaTLSSkipVerify,
    80  			Value: config.FormatBool(cfg.TLS.SkipVerify),
    81  		},
    82  		config.KV{
    83  			Key:   target.KafkaTLSClientAuth,
    84  			Value: strconv.Itoa(int(cfg.TLS.ClientAuth)),
    85  		},
    86  		config.KV{
    87  			Key:   target.KafkaSASL,
    88  			Value: config.FormatBool(cfg.SASL.Enable),
    89  		},
    90  		config.KV{
    91  			Key:   target.KafkaSASLUsername,
    92  			Value: cfg.SASL.User,
    93  		},
    94  		config.KV{
    95  			Key:   target.KafkaSASLPassword,
    96  			Value: cfg.SASL.Password,
    97  		},
    98  		config.KV{
    99  			Key:   target.KafkaCompressionCodec,
   100  			Value: cfg.Producer.Compression,
   101  		},
   102  		config.KV{
   103  			Key:   target.KafkaCompressionLevel,
   104  			Value: strconv.Itoa(cfg.Producer.CompressionLevel),
   105  		},
   106  	}
   107  	return nil
   108  }
   109  
   110  // SetNotifyAMQP - helper for config migration from older config.
   111  func SetNotifyAMQP(s config.Config, amqpName string, cfg target.AMQPArgs) error {
   112  	if !cfg.Enable {
   113  		return nil
   114  	}
   115  
   116  	if err := cfg.Validate(); err != nil {
   117  		return err
   118  	}
   119  
   120  	s[config.NotifyAMQPSubSys][amqpName] = config.KVS{
   121  		config.KV{
   122  			Key:   config.Enable,
   123  			Value: config.EnableOn,
   124  		},
   125  		config.KV{
   126  			Key:   target.AmqpURL,
   127  			Value: cfg.URL.String(),
   128  		},
   129  		config.KV{
   130  			Key:   target.AmqpExchange,
   131  			Value: cfg.Exchange,
   132  		},
   133  		config.KV{
   134  			Key:   target.AmqpRoutingKey,
   135  			Value: cfg.RoutingKey,
   136  		},
   137  		config.KV{
   138  			Key:   target.AmqpExchangeType,
   139  			Value: cfg.ExchangeType,
   140  		},
   141  		config.KV{
   142  			Key:   target.AmqpDeliveryMode,
   143  			Value: strconv.Itoa(int(cfg.DeliveryMode)),
   144  		},
   145  		config.KV{
   146  			Key:   target.AmqpMandatory,
   147  			Value: config.FormatBool(cfg.Mandatory),
   148  		},
   149  		config.KV{
   150  			Key:   target.AmqpInternal,
   151  			Value: config.FormatBool(cfg.Immediate),
   152  		},
   153  		config.KV{
   154  			Key:   target.AmqpDurable,
   155  			Value: config.FormatBool(cfg.Durable),
   156  		},
   157  		config.KV{
   158  			Key:   target.AmqpNoWait,
   159  			Value: config.FormatBool(cfg.NoWait),
   160  		},
   161  		config.KV{
   162  			Key:   target.AmqpAutoDeleted,
   163  			Value: config.FormatBool(cfg.AutoDeleted),
   164  		},
   165  		config.KV{
   166  			Key:   target.AmqpQueueDir,
   167  			Value: cfg.QueueDir,
   168  		},
   169  		config.KV{
   170  			Key:   target.AmqpQueueLimit,
   171  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   172  		},
   173  	}
   174  
   175  	return nil
   176  }
   177  
   178  // SetNotifyES - helper for config migration from older config.
   179  func SetNotifyES(s config.Config, esName string, cfg target.ElasticsearchArgs) error {
   180  	if !cfg.Enable {
   181  		return nil
   182  	}
   183  
   184  	if err := cfg.Validate(); err != nil {
   185  		return err
   186  	}
   187  
   188  	s[config.NotifyESSubSys][esName] = config.KVS{
   189  		config.KV{
   190  			Key:   config.Enable,
   191  			Value: config.EnableOn,
   192  		},
   193  		config.KV{
   194  			Key:   target.ElasticFormat,
   195  			Value: cfg.Format,
   196  		},
   197  		config.KV{
   198  			Key:   target.ElasticURL,
   199  			Value: cfg.URL.String(),
   200  		},
   201  		config.KV{
   202  			Key:   target.ElasticIndex,
   203  			Value: cfg.Index,
   204  		},
   205  		config.KV{
   206  			Key:   target.ElasticQueueDir,
   207  			Value: cfg.QueueDir,
   208  		},
   209  		config.KV{
   210  			Key:   target.ElasticQueueLimit,
   211  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   212  		},
   213  		config.KV{
   214  			Key:   target.ElasticUsername,
   215  			Value: cfg.Username,
   216  		},
   217  		config.KV{
   218  			Key:   target.ElasticPassword,
   219  			Value: cfg.Password,
   220  		},
   221  	}
   222  
   223  	return nil
   224  }
   225  
   226  // SetNotifyRedis - helper for config migration from older config.
   227  func SetNotifyRedis(s config.Config, redisName string, cfg target.RedisArgs) error {
   228  	if !cfg.Enable {
   229  		return nil
   230  	}
   231  
   232  	if err := cfg.Validate(); err != nil {
   233  		return err
   234  	}
   235  
   236  	s[config.NotifyRedisSubSys][redisName] = config.KVS{
   237  		config.KV{
   238  			Key:   config.Enable,
   239  			Value: config.EnableOn,
   240  		},
   241  		config.KV{
   242  			Key:   target.RedisFormat,
   243  			Value: cfg.Format,
   244  		},
   245  		config.KV{
   246  			Key:   target.RedisAddress,
   247  			Value: cfg.Addr.String(),
   248  		},
   249  		config.KV{
   250  			Key:   target.RedisPassword,
   251  			Value: cfg.Password,
   252  		},
   253  		config.KV{
   254  			Key:   target.RedisUser,
   255  			Value: cfg.User,
   256  		},
   257  		config.KV{
   258  			Key:   target.RedisKey,
   259  			Value: cfg.Key,
   260  		},
   261  		config.KV{
   262  			Key:   target.RedisQueueDir,
   263  			Value: cfg.QueueDir,
   264  		},
   265  		config.KV{
   266  			Key:   target.RedisQueueLimit,
   267  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   268  		},
   269  	}
   270  
   271  	return nil
   272  }
   273  
   274  // SetNotifyWebhook - helper for config migration from older config.
   275  func SetNotifyWebhook(s config.Config, whName string, cfg target.WebhookArgs) error {
   276  	if !cfg.Enable {
   277  		return nil
   278  	}
   279  
   280  	if err := cfg.Validate(); err != nil {
   281  		return err
   282  	}
   283  
   284  	s[config.NotifyWebhookSubSys][whName] = config.KVS{
   285  		config.KV{
   286  			Key:   config.Enable,
   287  			Value: config.EnableOn,
   288  		},
   289  		config.KV{
   290  			Key:   target.WebhookEndpoint,
   291  			Value: cfg.Endpoint.String(),
   292  		},
   293  		config.KV{
   294  			Key:   target.WebhookAuthToken,
   295  			Value: cfg.AuthToken,
   296  		},
   297  		config.KV{
   298  			Key:   target.WebhookQueueDir,
   299  			Value: cfg.QueueDir,
   300  		},
   301  		config.KV{
   302  			Key:   target.WebhookQueueLimit,
   303  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   304  		},
   305  		config.KV{
   306  			Key:   target.WebhookClientCert,
   307  			Value: cfg.ClientCert,
   308  		},
   309  		config.KV{
   310  			Key:   target.WebhookClientKey,
   311  			Value: cfg.ClientKey,
   312  		},
   313  	}
   314  
   315  	return nil
   316  }
   317  
   318  // SetNotifyPostgres - helper for config migration from older config.
   319  func SetNotifyPostgres(s config.Config, psqName string, cfg target.PostgreSQLArgs) error {
   320  	if !cfg.Enable {
   321  		return nil
   322  	}
   323  
   324  	if err := cfg.Validate(); err != nil {
   325  		return err
   326  	}
   327  
   328  	s[config.NotifyPostgresSubSys][psqName] = config.KVS{
   329  		config.KV{
   330  			Key:   config.Enable,
   331  			Value: config.EnableOn,
   332  		},
   333  		config.KV{
   334  			Key:   target.PostgresFormat,
   335  			Value: cfg.Format,
   336  		},
   337  		config.KV{
   338  			Key:   target.PostgresConnectionString,
   339  			Value: cfg.ConnectionString,
   340  		},
   341  		config.KV{
   342  			Key:   target.PostgresTable,
   343  			Value: cfg.Table,
   344  		},
   345  		config.KV{
   346  			Key:   target.PostgresHost,
   347  			Value: cfg.Host.String(),
   348  		},
   349  		config.KV{
   350  			Key:   target.PostgresPort,
   351  			Value: cfg.Port,
   352  		},
   353  		config.KV{
   354  			Key:   target.PostgresUsername,
   355  			Value: cfg.Username,
   356  		},
   357  		config.KV{
   358  			Key:   target.PostgresPassword,
   359  			Value: cfg.Password,
   360  		},
   361  		config.KV{
   362  			Key:   target.PostgresDatabase,
   363  			Value: cfg.Database,
   364  		},
   365  		config.KV{
   366  			Key:   target.PostgresQueueDir,
   367  			Value: cfg.QueueDir,
   368  		},
   369  		config.KV{
   370  			Key:   target.PostgresQueueLimit,
   371  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   372  		},
   373  		config.KV{
   374  			Key:   target.PostgresMaxOpenConnections,
   375  			Value: strconv.Itoa(cfg.MaxOpenConnections),
   376  		},
   377  	}
   378  
   379  	return nil
   380  }
   381  
   382  // SetNotifyNSQ - helper for config migration from older config.
   383  func SetNotifyNSQ(s config.Config, nsqName string, cfg target.NSQArgs) error {
   384  	if !cfg.Enable {
   385  		return nil
   386  	}
   387  
   388  	if err := cfg.Validate(); err != nil {
   389  		return err
   390  	}
   391  
   392  	s[config.NotifyNSQSubSys][nsqName] = config.KVS{
   393  		config.KV{
   394  			Key:   config.Enable,
   395  			Value: config.EnableOn,
   396  		},
   397  		config.KV{
   398  			Key:   target.NSQAddress,
   399  			Value: cfg.NSQDAddress.String(),
   400  		},
   401  		config.KV{
   402  			Key:   target.NSQTopic,
   403  			Value: cfg.Topic,
   404  		},
   405  		config.KV{
   406  			Key:   target.NSQTLS,
   407  			Value: config.FormatBool(cfg.TLS.Enable),
   408  		},
   409  		config.KV{
   410  			Key:   target.NSQTLSSkipVerify,
   411  			Value: config.FormatBool(cfg.TLS.SkipVerify),
   412  		},
   413  		config.KV{
   414  			Key:   target.NSQQueueDir,
   415  			Value: cfg.QueueDir,
   416  		},
   417  		config.KV{
   418  			Key:   target.NSQQueueLimit,
   419  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   420  		},
   421  	}
   422  
   423  	return nil
   424  }
   425  
   426  // SetNotifyNATS - helper for config migration from older config.
   427  func SetNotifyNATS(s config.Config, natsName string, cfg target.NATSArgs) error {
   428  	if !cfg.Enable {
   429  		return nil
   430  	}
   431  
   432  	if err := cfg.Validate(); err != nil {
   433  		return err
   434  	}
   435  
   436  	s[config.NotifyNATSSubSys][natsName] = config.KVS{
   437  		config.KV{
   438  			Key:   config.Enable,
   439  			Value: config.EnableOn,
   440  		},
   441  		config.KV{
   442  			Key:   target.NATSAddress,
   443  			Value: cfg.Address.String(),
   444  		},
   445  		config.KV{
   446  			Key:   target.NATSSubject,
   447  			Value: cfg.Subject,
   448  		},
   449  		config.KV{
   450  			Key:   target.NATSUsername,
   451  			Value: cfg.Username,
   452  		},
   453  		config.KV{
   454  			Key:   target.NATSUserCredentials,
   455  			Value: cfg.UserCredentials,
   456  		},
   457  		config.KV{
   458  			Key:   target.NATSPassword,
   459  			Value: cfg.Password,
   460  		},
   461  		config.KV{
   462  			Key:   target.NATSToken,
   463  			Value: cfg.Token,
   464  		},
   465  		config.KV{
   466  			Key:   target.NATSCertAuthority,
   467  			Value: cfg.CertAuthority,
   468  		},
   469  		config.KV{
   470  			Key:   target.NATSClientCert,
   471  			Value: cfg.ClientCert,
   472  		},
   473  		config.KV{
   474  			Key:   target.NATSClientKey,
   475  			Value: cfg.ClientKey,
   476  		},
   477  		config.KV{
   478  			Key:   target.NATSTLS,
   479  			Value: config.FormatBool(cfg.Secure),
   480  		},
   481  		config.KV{
   482  			Key:   target.NATSTLSSkipVerify,
   483  			Value: config.FormatBool(cfg.Secure),
   484  		},
   485  		config.KV{
   486  			Key:   target.NATSPingInterval,
   487  			Value: strconv.FormatInt(cfg.PingInterval, 10),
   488  		},
   489  		config.KV{
   490  			Key:   target.NATSQueueDir,
   491  			Value: cfg.QueueDir,
   492  		},
   493  		config.KV{
   494  			Key:   target.NATSQueueLimit,
   495  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   496  		},
   497  		config.KV{
   498  			Key: target.NATSStreaming,
   499  			Value: func() string {
   500  				if cfg.Streaming.Enable {
   501  					return config.EnableOn
   502  				}
   503  				return config.EnableOff
   504  			}(),
   505  		},
   506  		config.KV{
   507  			Key:   target.NATSStreamingClusterID,
   508  			Value: cfg.Streaming.ClusterID,
   509  		},
   510  		config.KV{
   511  			Key:   target.NATSStreamingAsync,
   512  			Value: config.FormatBool(cfg.Streaming.Async),
   513  		},
   514  		config.KV{
   515  			Key:   target.NATSStreamingMaxPubAcksInFlight,
   516  			Value: strconv.Itoa(cfg.Streaming.MaxPubAcksInflight),
   517  		},
   518  	}
   519  
   520  	return nil
   521  }
   522  
   523  // SetNotifyMySQL - helper for config migration from older config.
   524  func SetNotifyMySQL(s config.Config, sqlName string, cfg target.MySQLArgs) error {
   525  	if !cfg.Enable {
   526  		return nil
   527  	}
   528  
   529  	if err := cfg.Validate(); err != nil {
   530  		return err
   531  	}
   532  
   533  	s[config.NotifyMySQLSubSys][sqlName] = config.KVS{
   534  		config.KV{
   535  			Key:   config.Enable,
   536  			Value: config.EnableOn,
   537  		},
   538  		config.KV{
   539  			Key:   target.MySQLFormat,
   540  			Value: cfg.Format,
   541  		},
   542  		config.KV{
   543  			Key:   target.MySQLDSNString,
   544  			Value: cfg.DSN,
   545  		},
   546  		config.KV{
   547  			Key:   target.MySQLTable,
   548  			Value: cfg.Table,
   549  		},
   550  		config.KV{
   551  			Key:   target.MySQLHost,
   552  			Value: cfg.Host.String(),
   553  		},
   554  		config.KV{
   555  			Key:   target.MySQLPort,
   556  			Value: cfg.Port,
   557  		},
   558  		config.KV{
   559  			Key:   target.MySQLUsername,
   560  			Value: cfg.User,
   561  		},
   562  		config.KV{
   563  			Key:   target.MySQLPassword,
   564  			Value: cfg.Password,
   565  		},
   566  		config.KV{
   567  			Key:   target.MySQLDatabase,
   568  			Value: cfg.Database,
   569  		},
   570  		config.KV{
   571  			Key:   target.MySQLQueueDir,
   572  			Value: cfg.QueueDir,
   573  		},
   574  		config.KV{
   575  			Key:   target.MySQLQueueLimit,
   576  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   577  		},
   578  		config.KV{
   579  			Key:   target.MySQLMaxOpenConnections,
   580  			Value: strconv.Itoa(cfg.MaxOpenConnections),
   581  		},
   582  	}
   583  
   584  	return nil
   585  }
   586  
   587  // SetNotifyMQTT - helper for config migration from older config.
   588  func SetNotifyMQTT(s config.Config, mqttName string, cfg target.MQTTArgs) error {
   589  	if !cfg.Enable {
   590  		return nil
   591  	}
   592  
   593  	if err := cfg.Validate(); err != nil {
   594  		return err
   595  	}
   596  
   597  	s[config.NotifyMQTTSubSys][mqttName] = config.KVS{
   598  		config.KV{
   599  			Key:   config.Enable,
   600  			Value: config.EnableOn,
   601  		},
   602  		config.KV{
   603  			Key:   target.MqttBroker,
   604  			Value: cfg.Broker.String(),
   605  		},
   606  		config.KV{
   607  			Key:   target.MqttTopic,
   608  			Value: cfg.Topic,
   609  		},
   610  		config.KV{
   611  			Key:   target.MqttQoS,
   612  			Value: fmt.Sprintf("%d", cfg.QoS),
   613  		},
   614  		config.KV{
   615  			Key:   target.MqttUsername,
   616  			Value: cfg.User,
   617  		},
   618  		config.KV{
   619  			Key:   target.MqttPassword,
   620  			Value: cfg.Password,
   621  		},
   622  		config.KV{
   623  			Key:   target.MqttReconnectInterval,
   624  			Value: cfg.MaxReconnectInterval.String(),
   625  		},
   626  		config.KV{
   627  			Key:   target.MqttKeepAliveInterval,
   628  			Value: cfg.KeepAlive.String(),
   629  		},
   630  		config.KV{
   631  			Key:   target.MqttQueueDir,
   632  			Value: cfg.QueueDir,
   633  		},
   634  		config.KV{
   635  			Key:   target.MqttQueueLimit,
   636  			Value: strconv.Itoa(int(cfg.QueueLimit)),
   637  		},
   638  	}
   639  
   640  	return nil
   641  }