{"id":27775785,"url":"https://github.com/mandelsoft/ctxmgmt","last_synced_at":"2025-04-30T04:21:49.230Z","repository":{"id":290418095,"uuid":"972822965","full_name":"mandelsoft/ctxmgmt","owner":"mandelsoft","description":"A Generic and Extensible Configuration and Credential Framework for Go","archived":false,"fork":false,"pushed_at":"2025-04-28T18:03:24.000Z","size":581,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T18:43:44.077Z","etag":null,"topics":["configuration-management","credential-management","golang","golang-library","golang-package"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mandelsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-25T18:12:04.000Z","updated_at":"2025-04-28T18:03:28.000Z","dependencies_parsed_at":"2025-04-28T18:54:29.397Z","dependency_job_id":null,"html_url":"https://github.com/mandelsoft/ctxmgmt","commit_stats":null,"previous_names":["mandelsoft/ctxmgmt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fctxmgmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fctxmgmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fctxmgmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mandelsoft%2Fctxmgmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mandelsoft","download_url":"https://codeload.github.com/mandelsoft/ctxmgmt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251639659,"owners_count":21619810,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["configuration-management","credential-management","golang","golang-library","golang-package"],"created_at":"2025-04-30T04:21:48.709Z","updated_at":"2025-04-30T04:21:49.225Z","avatar_url":"https://github.com/mandelsoft.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- DO NOT MODIFY                   --\u003e\n\u003c!-- this file is generated by mdref --\u003e\n\u003c!-- from docsrc/README.md           --\u003e\n\n# A Generic and Extensible Configuration and Credential Framework for Go\n\nThis module provides some generic and extensible  frameworks for\n- managing configurations by bringing together configuration providers and configuration consumers in a generic and uniform way.\n  Any kinds of configuration settings can be combined and consistently\n  forwarded to appropriate configuration targets.  This way, although any used library may use own configuration settings, the using program can use a single uniform way to configure all those different used environments.\n- managing credentials by bringing together arbitrary credential sources, like config files or a vault credential repository and any kind of credential consumer. Hereby, programs don't nod to bother about reading various credential sources and forwarding appropriate credentials to used libraries. They just have to use the credential management and pass a generic credential context to potential credential consumer. This context then is able to provide appropriate crednetials according to the needs of the consumer.\n- data contexts responsible for a particular technical realm bundling the access API for this realm with configuration settings. Both, the configuration and the credential management are implemented ad data contexts using this framework. Hereby, the credential management context uses and incorporates a configuration context for its configuration needs\n\n## Example \n\nUsing the provided frameworks allows to bundle\nall required configuration in one central configuration file.\nIt may contain one or any number of arbitrary, but typed\nconfiguration data structures in form of a YAML document.\nIn our example this is a `.appconfig` file\n\n```yaml\ntype: generic.config.mandelsoft.de/v1\nconfigurations:\n- type: credentials.config.mandelsoft.de\n  consumers:\n      - identity:\n          type: service.acme.corp\n          hostname: localhost\n        credentials:\n          - type: Credentials\n            properties:\n              username: testuser\n              password: testpassword\n  repositories:\n          - repository:\n              type: DockerConfig/v1\n              dockerConfigFile: \"~/.docker/config.json\"\n              propagateConsumerIdentity: true\n\n- type: application.config.acme.org\n  serviceAddress: localhost:8080\n```\n\nThe credential management offers a config object of type\n`credentials.config.mandelsoft.de/v1`. \nIt can be used to configure supported credential repositories, here a docker config file. Arbitrary credential repositories types can be \nadded by using libraries. Out-of-the box, HasiCorp Vault, Docker and NPM config files are supported. Additionally,\nexplicit credential setting can be configured, here, credentials for a consumer type `service.acme.corp` \nused in out example.\n\nA second config object (`application.config.acme.org`), implemented by this example,\ncovers the configuration parameters for our demo application.\n\nThe application program now does not need to bother\nwith config or credential handling anymore.\nWhen starting, it just uses the config file to configure\nthe used credential context:\n\n```go\nfunc RunApplication() error {\n\n\tctx := credentials.New(ctxmgmt.MODE_SHARED)\n\n\terr := cfgutils.Configure(ctx, \".appconfig\")\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"reading configuration\")\n\t}\n\n\tapp, err := application.NewApplication(ctx)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"error creating application\")\n\t}\n\n\tapp.Describe()\n\treturn nil\n}\n```\n\nThis context is then passed around, and can be used by all participants to gain access to the required information.\n\nThe application constructor uses an own configuration structure, which can be served by our application config object. Calling `ApplyAllTo` applies all pending\nconfiguration objects applicable for the given configuration target.\n\n```go\ntype Config struct {\n\taddress string\n}\n\nfunc (a *Config) SetServiceAddress(addr string) {\n\ta.address = addr\n}\n\ntype Application struct {\n\tConfig\n\tclient *service.ServiceClient\n}\n\nvar _ myconfig.ConfigTarget = (*Application)(nil)\n\nfunc NewApplication(ctx credentials.Context) (*Application, error) {\n\tapp := \u0026Application{}\n\n\terr := ctx.ConfigContext().ApplyAllTo(\u0026app.Config)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"cannot apply to application config\")\n\t}\n\tapp.client, err = service.NewServiceClient(ctx, app.address)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"cannot create service client\")\n\t}\n\treturn app, nil\n}\n```\n\nThis way the address of the service instance to use is configured. But it does not need to care about the service client. It also gets access to the context and is responsible on its own to retrieve the required additional information from it.\n\nIt creates a specification for the required credentials, consisting of a consumption type (our `service.acme.corp`) and additional fields concretely describing the scenario, here, the host and port name.\n\n\n```go\nfunc GetConsumerId(addr string) credentials.ConsumerIdentity {\n\th, p, s := oci.SplitLocator(addr)\n\treturn credentials.NewConsumerIdentity(CONSUMER_TYPE,\n\t\thostpath.ID_HOSTNAME, h,\n\t\thostpath.ID_PORT, p,\n\t\thostpath.ID_PATHPREFIX, s,\n\t)\n}\n```\n\nUsing this specification it just asks the credential management to determine the best matching credential setting extracted from one of the configured credential sources.\nThe credential context incorporated a configuration context, which is used to implicitly configure and update the used credential sources. The caller does not need to care about this. It just asks for credentials meeting the given constraints.\n\n```go\nfunc NewServiceClient(ctx credentials.Context, addr string) (*ServiceClient, error) {\n\tcreds, err := credentials.CredentialsForConsumer(ctx, identity.GetConsumerId(addr), identity.IdentityMatcher)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\ts := \u0026ServiceClient{\n\t\taddress: addr,\n\t}\n\n\tif creds != nil {\n\t\ts.username = creds.GetProperty(credentials.ATTR_USERNAME)\n\t\ts.password = creds.GetProperty(credentials.ATTR_PASSWORD)\n\t\ts.cert = creds.GetProperty(credentials.ATTR_CERTIFICATE)\n\t}\n\treturn s, nil\n}\n```\n\nFinally, our main program just prints the configuration \nconfigured for the application and the service client.\n\n```\nservice address: localhost:8080\nservice client:\n username:    testuser\n password:    testpassword\n certificate: \n```\n\nThe complete example can be found in [examples/working-with-credentials/00-application-scenario.go](00-application-scenario.go).\n\n\n## The Tour\nA tour demonstrates how to use those frameworks:\n- [using configuration contexts](examples/working-with-config/README.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmandelsoft%2Fctxmgmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmandelsoft%2Fctxmgmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmandelsoft%2Fctxmgmt/lists"}