forked from edgexfoundry/edgex-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(snap): Apply proxy's runtime config options after startup (edgexf…
…oundry#3856) * fix: add options subcommand to apply partial configuration * build: bump github.com/canonical/edgex-snap-hooks/v2 to skip runtime config during defer-startup install mode * feat: apply security-proxy runtime configurations after startup Signed-off-by: Farshid Tavakolizadeh <[email protected]> (cherry picked from commit 3825f82)
- Loading branch information
Showing
7 changed files
with
132 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2022 Canonical Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0' | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
hooks "github.com/canonical/edgex-snap-hooks/v2" | ||
) | ||
|
||
var cli *hooks.CtlCli = hooks.NewSnapCtl() | ||
|
||
func main() { | ||
// no subcommand, as called by snapd | ||
if len(os.Args) == 1 { | ||
// configure everything | ||
configure() | ||
return | ||
} | ||
|
||
subCommand := os.Args[1] | ||
switch subCommand { | ||
case "options": | ||
// configure options | ||
options() | ||
default: | ||
panic("Unknown CLI sub-command: " + subCommand) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2022 Canonical Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0' | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
hooks "github.com/canonical/edgex-snap-hooks/v2" | ||
) | ||
|
||
func applyConfigOptions(service string) error { | ||
envJSON, err := cli.Config(hooks.EnvConfig + "." + service) | ||
if err != nil { | ||
return fmt.Errorf("failed to read config options for %s: %v", service, err) | ||
} | ||
|
||
if envJSON != "" { | ||
hooks.Debug(fmt.Sprintf("edgexfoundry:configure-options: service: %s envJSON: %s", service, envJSON)) | ||
if err := hooks.HandleEdgeXConfig(service, envJSON, nil); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// options is called by the main function to configure options | ||
func options() { | ||
flagset := flag.NewFlagSet("options", flag.ExitOnError) | ||
service := flagset.String("service", "", "Handle config options of a single service only") | ||
flagset.Parse(os.Args[2:]) | ||
|
||
fmt.Println("Configuring options for service: " + *service) | ||
|
||
debug, err := cli.Config("debug") | ||
if err != nil { | ||
fmt.Println(fmt.Sprintf("edgexfoundry:configure-options: can't read value of 'debug': %v", err)) | ||
os.Exit(1) | ||
} | ||
|
||
if err = hooks.Init(debug == "true", "edgexfoundry"); err != nil { | ||
fmt.Println(fmt.Sprintf("edgexfoundry:configure-options: initialization failure: %v", err)) | ||
os.Exit(1) | ||
} | ||
|
||
hooks.Info("edgexfoundry:configure-options: handling config options for a single service: " + *service) | ||
|
||
if err := applyConfigOptions(*service); err != nil { | ||
hooks.Error(fmt.Sprintf("edgexfoundry:configure-options: error handling config options for %s: %v", *service, err)) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module github.com/canonical/edgex-go/hooks | ||
|
||
require github.com/canonical/edgex-snap-hooks/v2 v2.1.1 | ||
require github.com/canonical/edgex-snap-hooks/v2 v2.1.3 | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
snap/local/runtime-helpers/bin/security-proxy-post-setup.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash -e | ||
|
||
# This script is called as a post-stop-command when | ||
# security-proxy-setup oneshot service stops. | ||
|
||
logger "edgexfoundry:security-proxy-post-setup" | ||
|
||
# add bin directory to have e.g. secret-config in PATH | ||
export PATH="$SNAP/bin:$PATH" | ||
|
||
# Several config options depend on resources that only exist after proxy | ||
# setup. This re-applies the config options logic after deferred startup: | ||
$SNAP/snap/hooks/configure options --service=security-proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters