Skip to content

Commit

Permalink
Guard against expiring tokens due to wrong database (#13693)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 4, 2024
1 parent eeb42c4 commit f4f7a90
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/charger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {

func runCharger(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/charger_ramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func ramp(c api.Charger, digits int, delay time.Duration) {

func runChargerRamp(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/check_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

func runConfigCheck(cmd *cobra.Command, args []string) {
err := loadConfigFile(&conf)
err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed)

if err != nil {
log.FATAL.Println("config invalid:", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {

func runDevice(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/discuss.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func errorString(err error) string {
}

func runDiscuss(cmd *cobra.Command, args []string) {
cfgErr := loadConfigFile(&conf)
cfgErr := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed)

file, pathErr := filepath.Abs(cfgFile)
if pathErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func handle[T any](name string, h config.Handler[T]) config.Device[T] {

func runDump(cmd *cobra.Command, args []string) {
// load config
err := loadConfigFile(&conf)
err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed)

// setup environment
if err == nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const (
flagHeaders = "log-headers"
flagHeadersDescription = "Log headers"

flagIgnoreDatabase = "ignore-db"
flagIgnoreDatabaseDescription = "Run command ignoring service database"

flagBatteryMode = "battery-mode"
flagBatteryModeDescription = "Set battery mode (normal, hold, charge)"
flagBatteryModeWait = "battery-mode-wait"
Expand Down
2 changes: 1 addition & 1 deletion cmd/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {

func runMeter(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/password_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {

func runPasswordReset(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/password_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {

func runPasswordSet(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import (
"github.com/spf13/viper"
)

const rebootDelay = 5 * time.Minute // delayed reboot on error
const (
rebootDelay = 5 * time.Minute // delayed reboot on error
serviceDB = "/var/lib/evcc/evcc.db"
)

var (
log = util.NewLogger("main")
Expand All @@ -53,10 +56,9 @@ func init() {

// global options
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")")

rootCmd.PersistentFlags().BoolP("help", "h", false, "Help")

rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
rootCmd.PersistentFlags().Bool(flagIgnoreDatabase, false, flagIgnoreDatabaseDescription)

// config file options
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
Expand Down Expand Up @@ -107,7 +109,7 @@ func Execute() {
func runRoot(cmd *cobra.Command, args []string) {
// load config and re-configure logging after reading config file
var err error
if cfgErr := loadConfigFile(&conf); errors.As(cfgErr, &viper.ConfigFileNotFoundError{}) {
if cfgErr := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); errors.As(cfgErr, &viper.ConfigFileNotFoundError{}) {
log.INFO.Println("missing config file - switching into demo mode")
if err := demoConfig(&conf); err != nil {
log.FATAL.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/settings-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {

func runSettingsGet(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/settings-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {

func runSettingsSet(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
32 changes: 31 additions & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"net/http"
"os"
"regexp"
"slices"
"strconv"
Expand Down Expand Up @@ -162,7 +163,23 @@ func nameValid(name string) error {
return nil
}

func loadConfigFile(conf *globalConfig) error {
func tokenDanger(conf []config.Named) bool {
problematic := []string{"tesla", "psa", "opel", "citroen", "ds", "peugeot"}

for _, cc := range conf {
if slices.Contains(problematic, cc.Type) {
return true
}
template, ok := cc.Other["template"].(string)
if ok && cc.Type == "template" && slices.Contains(problematic, template) {
return true
}
}

return false
}

func loadConfigFile(conf *globalConfig, checkDB bool) error {
err := viper.ReadInConfig()

if cfgFile = viper.ConfigFileUsed(); cfgFile == "" {
Expand All @@ -177,6 +194,19 @@ func loadConfigFile(conf *globalConfig) error {
}
}

// check service database
if _, err := os.Stat(serviceDB); err == nil && checkDB && conf.Database.Dsn != serviceDB && tokenDanger(conf.Vehicles) {
log.FATAL.Fatal(`
Found systemd service database at "` + serviceDB + `", evcc has been invoked with database "` + conf.Database.Dsn + `".
Running evcc with vehicles configured in evcc.yaml may lead to expiring the yaml configuration's vehicle tokens.
This is due to the fact, that the token refresh will be saved to the local instead of the service's database.
If you have vehicles with touchy tokens like PSA or Tesla, make sure to remove vehicle configuration from the yaml file.
If you know what you're doing, you can run evcc ignoring the service database with the --ignore-db flag.
`)
}

// parse log levels after reading config
if err == nil {
parseLogLevels()
Expand Down
2 changes: 1 addition & 1 deletion cmd/tariff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {

func runTariff(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {

func runToken(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
log.FATAL.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {

func runVehicle(cmd *cobra.Command, args []string) {
// load config
if err := loadConfigFile(&conf); err != nil {
if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil {
fatal(err)
}

Expand Down

0 comments on commit f4f7a90

Please sign in to comment.