@@ -48,20 +48,30 @@ func (a AuthConfig) Base64() (string, error) {
48
48
return base64 .URLEncoding .EncodeToString (authStr ), nil
49
49
}
50
50
51
- func AuthConfigFromPath (path string , registry string ) (AuthConfig , error ) {
51
+ func AuthConfigFromPath (path string , reg string ) (AuthConfig , error ) {
52
52
var config dockercfg.Config
53
53
err := dockercfg .FromFile (path , & config )
54
54
if err != nil {
55
55
return AuthConfig {}, xerrors .Errorf ("load config: %w" , err )
56
56
}
57
57
58
- hostname := dockercfg .ResolveRegistryHost (registry )
58
+ return parseConfig (config , reg )
59
+ }
59
60
60
- if config , ok := config .AuthConfigs [registry ]; ok {
61
- return AuthConfig (config ), nil
61
+ func AuthConfigFromString (raw string , reg string ) (AuthConfig , error ) {
62
+ var cfg dockercfg.Config
63
+ err := json .Unmarshal ([]byte (raw ), & cfg )
64
+ if err != nil {
65
+ return AuthConfig {}, xerrors .Errorf ("parse config: %w" , err )
62
66
}
67
+ return parseConfig (cfg , reg )
68
+ }
69
+
70
+ func parseConfig (cfg dockercfg.Config , registry string ) (AuthConfig , error ) {
71
+
72
+ hostname := dockercfg .ResolveRegistryHost (registry )
63
73
64
- username , secret , err := config .GetRegistryCredentials (hostname )
74
+ username , secret , err := cfg .GetRegistryCredentials (hostname )
65
75
if err != nil {
66
76
return AuthConfig {}, xerrors .Errorf ("get credentials from helper: %w" , err )
67
77
}
@@ -79,23 +89,5 @@ func AuthConfigFromPath(path string, registry string) (AuthConfig, error) {
79
89
}
80
90
81
91
return AuthConfig {}, xerrors .Errorf ("no auth config found for registry %s: %w" , registry , os .ErrNotExist )
82
- }
83
-
84
- func ParseAuthConfig (raw string ) (AuthConfig , error ) {
85
- type dockerConfig struct {
86
- AuthConfigs map [string ]dockertypes.AuthConfig `json:"auths"`
87
- }
88
-
89
- var conf dockerConfig
90
- if err := json .Unmarshal ([]byte (raw ), & conf ); err != nil {
91
- return AuthConfig {}, xerrors .Errorf ("parse docker auth config secret: %w" , err )
92
- }
93
- if len (conf .AuthConfigs ) != 1 {
94
- return AuthConfig {}, xerrors .Errorf ("number of image pull auth configs not equal to 1 (%d)" , len (conf .AuthConfigs ))
95
- }
96
- for _ , regConfig := range conf .AuthConfigs {
97
- return AuthConfig (regConfig ), nil
98
- }
99
92
100
- return AuthConfig {}, xerrors .New ("no auth configs parsed." )
101
93
}
0 commit comments