Config Manager: What is it, why is it used and how does it work?
Config manager is a tool used to manage the configuration settings of applications. These tools ensure that settings are stored and managed consistently and securely in different environments (development, testing, production).
There are many benefits to using a config manager:
- Central management of settings: All settings are stored in one place, ensuring they remain consistent and up-to-date.
- Environment-based configuration: You can set different settings for different environments.
- Security: Sensitive settings can be stored encrypted.
- Easy management: Settings can be changed independently of the code.
- Dynamic config update: Allows for the settings to be changed during runtime through external config manipulation.
A Simple Config Manager Application
Below are the codes for a simple config manager application written in Go:
Go
package main
import (
"fmt"
"github.com/spf13/viper"
)
func main() {
// Set the config file and format
viper.SetConfigFile("config.json")
viper.SetConfigType("json")
// Read the config file
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
// Read the config values
port := viper.GetInt("port")
databaseURL := viper.GetString("database_url")
// Use the config values
fmt.Println("Port:", port)
fmt.Println("Database URL:", databaseURL)
}
In this application:
- The
viper
library is used to read the config file and store the settings in memory. SetConfigFile("config.json")
andSetConfigType("json")
functions determine the name and format of the config file.- The
ReadInConfig()
function reads the config file and stores the settings in memory. GetInt("port")
andGetString("database_url")
functions read theport
anddatabase_url
settings from the config file.- The read settings are printed on the screen.
Example Config File
JSON
{
"port": 8080,
"database_url": "mongodb://localhost:27017/mydb"
}
In this config file, the port
setting is set to 8080 and the database_url
setting is set to mongodb://localhost:27017/mydb
.
External Config Manipulation
In the simple config manager example mentioned above, the config file is directly specified in the code. In this case, the application needs to be restarted to update the config file.
However, it is also possible to manipulate the config values externally during runtime. There are several ways to do this:
1. Environment Variables:
Environment variables can be used to share config values with the application. For example, the following command sets the port
setting to 8081:
export PORT=8081
The application can read the PORT
environment variable using the viper.GetInt("port")
function.
2. HTTP API:
An HTTP API can be used to update the config values. For example, the following API can be used to update the port
setting:
curl -X PUT -H "Content-Type: application/json" -d '{"port": 8081}' <http://localhost:8080/api/config>
3. Command Line Tool:
A command line tool can be used to update the config values. For example, the following command sets the port
setting to 8081:
./config-tool set port 8081
Benefits of external config manipulation:
- There is no need to restart the application to update the config file.
- You can change the config values dynamically during runtime.
External Config Manipulation
- You can change the config values dynamically during runtime.
- You can set different config values for different environments.
Security risks of external config manipulation:
- Unauthorized access can lead to changes in the config values.
- Incorrect config values can cause the application to malfunction.
For secure config manipulation:
- It is important to protect config values using methods such as encryption.
- It is important to use authorization mechanisms to control who can change the config values.
Example Application (with HTTP API)
Below is a simple example application for updating config values using an HTTP API:
Go
package main
import (
"fmt"
"net/http"
"github.com/spf13/viper"
)
func main() {
// Set the config file and format
viper.SetConfigFile("config.json")
viper.SetConfigType("json")
// Read the config file
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
// Start the HTTP API
http.HandleFunc("/api/config", updateConfigHandler)
http.ListenAndServe(":8080", nil)
}
func updateConfigHandler(w http.ResponseWriter, r *http.Request) {
// Update the config values
decoder := json.NewDecoder(r.Body)
var config map[string]interface{}
err := decoder.Decode(&config)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for key, value := range config {
viper.Set(key, value)
}
// Print the updated config values
fmt.Println("Config updated:", config)
}
In this application, the /api/config
endpoint can be used to update the config values. An HTTP POST request sends the updated config values in JSON format.
The application updates the config values using the viper
library and prints the updated config values on the screen.
Conclusion
Config Manager is a powerful tool for managing the configuration settings of applications. These tools ensure that settings are consistent, secure, and manageable.
- External config manipulation allows for the settings to be changed during runtime without having to restart the application to update the config file.
- Different config values can be set for different environments.
However, external config manipulation also brings security risks. It is important to protect the config values and control who can change them to minimize these risks.
More Information
- Go Documentation: https://go.dev/doc/
- Viper Library Documentation: https://github.com/spf13/viper
Additional Notes
- In this article, the basic concepts and working principles of the config manager have been covered.
- For more detailed information, you can refer to the articles and documentation provided as references.
- There are various usage scenarios and application examples of the config manager in different programming languages.
I hope this article has helped you understand and start using the config manager.