1
1
package client
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"io"
@@ -19,7 +20,9 @@ import (
19
20
"github.com/otiai10/copy"
20
21
"golang.org/x/mod/module"
21
22
"kcl-lang.io/kcl-go/pkg/kcl"
23
+ "oras.land/oras-go/pkg/auth"
22
24
"oras.land/oras-go/v2"
25
+ remoteauth "oras.land/oras-go/v2/registry/remote/auth"
23
26
24
27
"kcl-lang.io/kpm/pkg/constants"
25
28
"kcl-lang.io/kpm/pkg/downloader"
@@ -41,6 +44,8 @@ type KpmClient struct {
41
44
logWriter io.Writer
42
45
// The downloader of the dependencies.
43
46
DepDownloader * downloader.DepDownloader
47
+ // credential store
48
+ credsClient * downloader.CredClient
44
49
// The home path of kpm for global configuration file and kcl package storage path.
45
50
homePath string
46
51
// The settings of kpm loaded from the global configuration file.
@@ -75,6 +80,33 @@ func (c *KpmClient) SetNoSumCheck(noSumCheck bool) {
75
80
c .noSumCheck = noSumCheck
76
81
}
77
82
83
+ // GetCredsClient will return the credential client.
84
+ func (c * KpmClient ) GetCredsClient () (* downloader.CredClient , error ) {
85
+ if c .credsClient == nil {
86
+ credCli , err := downloader .LoadCredentialFile (c .settings .CredentialsFile )
87
+ if err != nil {
88
+ return nil , err
89
+ }
90
+ c .credsClient = credCli
91
+ }
92
+ return c .credsClient , nil
93
+ }
94
+
95
+ // GetCredentials will return the credentials of the host.
96
+ func (c * KpmClient ) GetCredentials (hostName string ) (* remoteauth.Credential , error ) {
97
+ credCli , err := c .GetCredsClient ()
98
+ if err != nil {
99
+ return nil , err
100
+ }
101
+
102
+ creds , err := credCli .Credential (hostName )
103
+ if err != nil {
104
+ return nil , err
105
+ }
106
+
107
+ return creds , nil
108
+ }
109
+
78
110
// GetNoSumCheck will return the 'noSumCheck' flag.
79
111
func (c * KpmClient ) GetNoSumCheck () bool {
80
112
return c .noSumCheck
@@ -953,7 +985,18 @@ func (c *KpmClient) FillDependenciesInfo(modFile *pkg.ModFile) error {
953
985
954
986
// AcquireTheLatestOciVersion will acquire the latest version of the OCI reference.
955
987
func (c * KpmClient ) AcquireTheLatestOciVersion (ociSource downloader.Oci ) (string , error ) {
956
- ociClient , err := oci .NewOciClient (ociSource .Reg , ociSource .Repo , & c .settings )
988
+ repoPath := utils .JoinPath (ociSource .Reg , ociSource .Repo )
989
+ cred , err := c .GetCredentials (ociSource .Reg )
990
+ if err != nil {
991
+ return "" , err
992
+ }
993
+
994
+ ociClient , err := oci .NewOciClientWithOpts (
995
+ oci .WithCredential (cred ),
996
+ oci .WithRepoPath (repoPath ),
997
+ oci .WithPlainHttp (c .GetSettings ().DefaultOciPlainHttp ()),
998
+ )
999
+
957
1000
if err != nil {
958
1001
return "" , err
959
1002
}
@@ -1098,11 +1141,16 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
1098
1141
// clean the temp dir.
1099
1142
defer os .RemoveAll (tmpDir )
1100
1143
1144
+ credCli , err := c .GetCredsClient ()
1145
+ if err != nil {
1146
+ return nil , err
1147
+ }
1101
1148
err = c .DepDownloader .Download (* downloader .NewDownloadOptions (
1102
1149
downloader .WithLocalPath (tmpDir ),
1103
1150
downloader .WithSource (dep .Source ),
1104
1151
downloader .WithLogWriter (c .logWriter ),
1105
1152
downloader .WithSettings (c .settings ),
1153
+ downloader .WithCredsClient (credCli ),
1106
1154
))
1107
1155
if err != nil {
1108
1156
return nil , err
@@ -1276,10 +1324,22 @@ func (c *KpmClient) ParseKclModFile(kclPkg *pkg.KclPkg) (map[string]map[string]s
1276
1324
1277
1325
// LoadPkgFromOci will download the kcl package from the oci repository and return an `KclPkg`.
1278
1326
func (c * KpmClient ) DownloadPkgFromOci (dep * downloader.Oci , localPath string ) (* pkg.KclPkg , error ) {
1279
- ociClient , err := oci .NewOciClient (dep .Reg , dep .Repo , & c .settings )
1327
+ repoPath := utils .JoinPath (dep .Reg , dep .Repo )
1328
+ cred , err := c .GetCredentials (dep .Reg )
1280
1329
if err != nil {
1281
1330
return nil , err
1282
1331
}
1332
+
1333
+ ociClient , err := oci .NewOciClientWithOpts (
1334
+ oci .WithCredential (cred ),
1335
+ oci .WithRepoPath (repoPath ),
1336
+ oci .WithPlainHttp (c .GetSettings ().DefaultOciPlainHttp ()),
1337
+ )
1338
+
1339
+ if err != nil {
1340
+ return nil , err
1341
+ }
1342
+
1283
1343
ociClient .SetLogWriter (c .logWriter )
1284
1344
// Select the latest tag, if the tag, the user inputed, is empty.
1285
1345
var tagSelected string
@@ -1478,7 +1538,18 @@ func (c *KpmClient) PullFromOci(localPath, source, tag string) error {
1478
1538
1479
1539
// PushToOci will push a kcl package to oci registry.
1480
1540
func (c * KpmClient ) PushToOci (localPath string , ociOpts * opt.OciOptions ) error {
1481
- ociCli , err := oci .NewOciClient (ociOpts .Reg , ociOpts .Repo , & c .settings )
1541
+ repoPath := utils .JoinPath (ociOpts .Reg , ociOpts .Repo )
1542
+ cred , err := c .GetCredentials (ociOpts .Reg )
1543
+ if err != nil {
1544
+ return err
1545
+ }
1546
+
1547
+ ociCli , err := oci .NewOciClientWithOpts (
1548
+ oci .WithCredential (cred ),
1549
+ oci .WithRepoPath (repoPath ),
1550
+ oci .WithPlainHttp (c .GetSettings ().DefaultOciPlainHttp ()),
1551
+ )
1552
+
1482
1553
if err != nil {
1483
1554
return err
1484
1555
}
@@ -1504,12 +1575,46 @@ func (c *KpmClient) PushToOci(localPath string, ociOpts *opt.OciOptions) error {
1504
1575
1505
1576
// LoginOci will login to the oci registry.
1506
1577
func (c * KpmClient ) LoginOci (hostname , username , password string ) error {
1507
- return oci .Login (hostname , username , password , & c .settings )
1578
+
1579
+ credCli , err := c .GetCredsClient ()
1580
+ if err != nil {
1581
+ return err
1582
+ }
1583
+
1584
+ err = credCli .GetAuthClient ().LoginWithOpts (
1585
+ []auth.LoginOption {
1586
+ auth .WithLoginHostname (hostname ),
1587
+ auth .WithLoginUsername (username ),
1588
+ auth .WithLoginSecret (password ),
1589
+ }... ,
1590
+ )
1591
+
1592
+ if err != nil {
1593
+ return reporter .NewErrorEvent (
1594
+ reporter .FailedLogin ,
1595
+ err ,
1596
+ fmt .Sprintf ("failed to login '%s', please check registry, username and password is valid" , hostname ),
1597
+ )
1598
+ }
1599
+
1600
+ return nil
1508
1601
}
1509
1602
1510
1603
// LogoutOci will logout from the oci registry.
1511
1604
func (c * KpmClient ) LogoutOci (hostname string ) error {
1512
- return oci .Logout (hostname , & c .settings )
1605
+
1606
+ credCli , err := c .GetCredsClient ()
1607
+ if err != nil {
1608
+ return err
1609
+ }
1610
+
1611
+ err = credCli .GetAuthClient ().Logout (context .Background (), hostname )
1612
+
1613
+ if err != nil {
1614
+ return reporter .NewErrorEvent (reporter .FailedLogout , err , fmt .Sprintf ("failed to logout '%s'" , hostname ))
1615
+ }
1616
+
1617
+ return nil
1513
1618
}
1514
1619
1515
1620
// ParseOciRef will parser '<repo_name>:<repo_tag>' into an 'OciOptions'.
@@ -1753,7 +1858,18 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
1753
1858
return reporter .NewErrorEvent (reporter .Bug , err )
1754
1859
}
1755
1860
1756
- ociCli , err := oci .NewOciClient (ociOpts .Reg , ociOpts .Repo , & c .settings )
1861
+ repoPath := utils .JoinPath (ociOpts .Reg , ociOpts .Repo )
1862
+ cred , err := c .GetCredentials (ociOpts .Reg )
1863
+ if err != nil {
1864
+ return err
1865
+ }
1866
+
1867
+ ociCli , err := oci .NewOciClientWithOpts (
1868
+ oci .WithCredential (cred ),
1869
+ oci .WithRepoPath (repoPath ),
1870
+ oci .WithPlainHttp (c .GetSettings ().DefaultOciPlainHttp ()),
1871
+ )
1872
+
1757
1873
if err != nil {
1758
1874
return err
1759
1875
}
@@ -1790,7 +1906,19 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
1790
1906
1791
1907
// FetchOciManifestConfIntoJsonStr will fetch the oci manifest config of the kcl package from the oci registry and return it into json string.
1792
1908
func (c * KpmClient ) FetchOciManifestIntoJsonStr (opts opt.OciFetchOptions ) (string , error ) {
1793
- ociCli , err := oci .NewOciClient (opts .Reg , opts .Repo , & c .settings )
1909
+
1910
+ repoPath := utils .JoinPath (opts .Reg , opts .Repo )
1911
+ cred , err := c .GetCredentials (opts .Reg )
1912
+ if err != nil {
1913
+ return "" , err
1914
+ }
1915
+
1916
+ ociCli , err := oci .NewOciClientWithOpts (
1917
+ oci .WithCredential (cred ),
1918
+ oci .WithRepoPath (repoPath ),
1919
+ oci .WithPlainHttp (c .GetSettings ().DefaultOciPlainHttp ()),
1920
+ )
1921
+
1794
1922
if err != nil {
1795
1923
return "" , err
1796
1924
}
0 commit comments