Skip to content

Commit 49967a2

Browse files
authored
Fix fallback manual code signing 2 (#229)
* Move devportalclient initislization and automatic code signing assets management into codesign.Manager.prepareAutomaticAssets * Add Login func to DevPortalClient * Fix ioutil deprecations
1 parent 603a30a commit 49967a2

File tree

17 files changed

+175
-106
lines changed

17 files changed

+175
-106
lines changed

autocodesign/autocodesign.go

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type Certificate struct {
6969

7070
// DevPortalClient abstract away the Apple Developer Portal API
7171
type DevPortalClient interface {
72+
Login() error
73+
7274
QueryCertificateBySerial(serial big.Int) (Certificate, error)
7375
QueryAllIOSCertificates() (map[appstoreconnect.CertificateType][]Certificate, error)
7476

autocodesign/certdownloader/certdownloader_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package certdownloader
22

33
import (
4-
"io/ioutil"
54
"net/http"
65
"net/http/httptest"
6+
"os"
77
"testing"
88
"time"
99

@@ -20,7 +20,7 @@ func Test_downloader_GetCertificates_Local(t *testing.T) {
2020
t.Errorf("init: failed to encode certificate: %s", err)
2121
}
2222

23-
p12File, err := ioutil.TempFile("", "*.p12")
23+
p12File, err := os.CreateTemp("", "*.p12")
2424
if err != nil {
2525
t.Errorf("init: failed to create temp test file: %s", err)
2626
}

autocodesign/codesignasset/writer.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package codesignasset
33

44
import (
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path"
98

@@ -101,7 +100,7 @@ func (w Writer) InstallProfile(profile autocodesign.Profile) error {
101100
}
102101

103102
name := path.Join(profilesDir, profile.Attributes().UUID+ext)
104-
if err := ioutil.WriteFile(name, profile.Attributes().ProfileContent, 0600); err != nil {
103+
if err := os.WriteFile(name, profile.Attributes().ProfileContent, 0600); err != nil {
105104
return fmt.Errorf("failed to write profile to file: %s", err)
106105
}
107106

autocodesign/devportalclient/appstoreconnect/appstoreconnect.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"net/http"
1413
"net/url"
1514
"reflect"
@@ -179,7 +178,7 @@ func checkResponse(r *http.Response) error {
179178
}
180179

181180
errorResponse := &ErrorResponse{Response: r}
182-
data, err := ioutil.ReadAll(r.Body)
181+
data, err := io.ReadAll(r.Body)
183182
if err == nil && data != nil {
184183
if err := json.Unmarshal(data, errorResponse); err != nil {
185184
log.Errorf("Failed to unmarshal response (%s): %s", string(data), err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package appstoreconnectclient
2+
3+
// AuthClient ...
4+
type AuthClient struct {
5+
}
6+
7+
// NewAuthClient ...
8+
func NewAuthClient() *AuthClient {
9+
return &AuthClient{}
10+
}
11+
12+
// Login ...
13+
func (c *AuthClient) Login() error {
14+
return nil
15+
}

autocodesign/devportalclient/appstoreconnectclient/client.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
// Client ...
1212
type Client struct {
13+
*AuthClient
1314
*CertificateSource
1415
*DeviceClient
1516
*ProfileClient
@@ -18,6 +19,7 @@ type Client struct {
1819
// NewAPIDevPortalClient ...
1920
func NewAPIDevPortalClient(client *appstoreconnect.Client) autocodesign.DevPortalClient {
2021
return Client{
22+
AuthClient: NewAuthClient(),
2123
CertificateSource: NewCertificateSource(client),
2224
DeviceClient: NewDeviceClient(client),
2325
ProfileClient: NewProfileClient(client),

autocodesign/devportalclient/appstoreconnectclient/profiles_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"testing"
1010

@@ -178,13 +178,13 @@ func newResponse(t *testing.T, status int, body map[string]interface{}) *http.Re
178178
resp := http.Response{
179179
StatusCode: status,
180180
Header: http.Header{},
181-
Body: ioutil.NopCloser(nil),
181+
Body: io.NopCloser(nil),
182182
}
183183

184184
if body != nil {
185185
var buff bytes.Buffer
186186
require.NoError(t, json.NewEncoder(&buff).Encode(body))
187-
resp.Body = ioutil.NopCloser(&buff)
187+
resp.Body = io.NopCloser(&buff)
188188
resp.ContentLength = int64(buff.Len())
189189
}
190190

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package spaceship
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// AuthClient ...
9+
type AuthClient struct {
10+
client *Client
11+
}
12+
13+
// NewAuthClient ...
14+
func NewAuthClient(client *Client) *AuthClient {
15+
return &AuthClient{client: client}
16+
}
17+
18+
// Login ...
19+
func (c *AuthClient) Login() error {
20+
output, err := c.client.runSpaceshipCommand("login")
21+
if err != nil {
22+
return fmt.Errorf("running command failed with error: %w", err)
23+
}
24+
25+
var teamIDResponse struct {
26+
Data string `json:"data"`
27+
}
28+
if err := json.Unmarshal([]byte(output), &teamIDResponse); err != nil {
29+
return fmt.Errorf("failed to unmarshal response: %w", err)
30+
}
31+
32+
c.client.teamID = teamIDResponse.Data
33+
return nil
34+
}

autocodesign/devportalclient/spaceship/spaceship.go

+4-30
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,17 @@ func NewClient(authConfig appleauth.AppleID, teamID string, cmdFactory ruby.Comm
4343
return nil, err
4444
}
4545

46-
c := &Client{
46+
return &Client{
4747
workDir: dir,
4848
authConfig: authConfig,
4949
teamID: teamID,
5050
cmdFactory: cmdFactory,
51-
}
52-
53-
currentTeamID, err := c.login()
54-
if err != nil {
55-
return nil, fmt.Errorf("spaceship command failed: %s", err)
56-
}
57-
58-
log.Debugf("current team id: %s", currentTeamID)
59-
60-
c.teamID = currentTeamID
61-
62-
return c, nil
63-
}
64-
65-
func (c *Client) login() (string, error) {
66-
output, err := c.runSpaceshipCommand("login")
67-
if err != nil {
68-
return "", fmt.Errorf("running command failed with error: %s", err)
69-
}
70-
71-
// {"data":"72SA8V3WYL"}
72-
var teamIDResponse struct {
73-
Data string `json:"data"`
74-
}
75-
if err := json.Unmarshal([]byte(output), &teamIDResponse); err != nil {
76-
return "", fmt.Errorf("failed to unmarshal response: %v", err)
77-
}
78-
79-
return teamIDResponse.Data, nil
51+
}, nil
8052
}
8153

8254
// DevPortalClient ...
8355
type DevPortalClient struct {
56+
*AuthClient
8457
*CertificateSource
8558
*ProfileClient
8659
*DeviceClient
@@ -89,6 +62,7 @@ type DevPortalClient struct {
8962
// NewSpaceshipDevportalClient ...
9063
func NewSpaceshipDevportalClient(client *Client) autocodesign.DevPortalClient {
9164
return DevPortalClient{
65+
AuthClient: NewAuthClient(client),
9266
CertificateSource: NewSpaceshipCertificateSource(client),
9367
DeviceClient: NewDeviceClient(client),
9468
ProfileClient: NewSpaceshipProfileClient(client),

autocodesign/keychain/keychain_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package keychain
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"testing"
@@ -14,7 +13,7 @@ import (
1413
)
1514

1615
func TestCreateKeychain(t *testing.T) {
17-
dir, err := ioutil.TempDir("", "test-create-keychain")
16+
dir, err := os.MkdirTemp("", "test-create-keychain")
1817
if err != nil {
1918
t.Errorf("setup: create temp dir for keychain: %s", err)
2019
}
@@ -38,7 +37,7 @@ func TestKeychain_importCertificate(t *testing.T) {
3837
)
3938

4039
// Create test keychain
41-
dirTmp, err := ioutil.TempDir("", "test-import-certificate")
40+
dirTmp, err := os.MkdirTemp("", "test-import-certificate")
4241
if err != nil {
4342
t.Fatalf("setup: create temp dir for keychain: %s", err)
4443
}
@@ -82,7 +81,7 @@ func TestKeychain_importCertificate(t *testing.T) {
8281
}
8382

8483
testcertPath := filepath.Join(dirTmp, "TestCert.p12")
85-
if err := ioutil.WriteFile(testcertPath, pfxData, 0600); err != nil {
84+
if err := os.WriteFile(testcertPath, pfxData, 0600); err != nil {
8685
t.Fatalf("Setup: failed to write test p12 file.")
8786
}
8887

autocodesign/localcodesignasset/profileconverter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package localcodesignasset
22

33
import (
4-
"io/ioutil"
4+
"os"
55

66
"github.com/bitrise-io/go-xcode/profileutil"
77
"github.com/bitrise-io/go-xcode/v2/autocodesign"
@@ -26,7 +26,7 @@ func (c provisioningProfileConverter) ProfileInfoToProfile(info profileutil.Prov
2626
if err != nil {
2727
return nil, err
2828
}
29-
content, err := ioutil.ReadFile(pth)
29+
content, err := os.ReadFile(pth)
3030
if err != nil {
3131
return nil, err
3232
}

0 commit comments

Comments
 (0)