@@ -75,11 +75,11 @@ func (o *osFs) ReadlinkIfPossible(name string) (string, error) {
7575}
7676
7777type Device interface {
78- EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , error )
78+ EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , bool , error )
7979 MappedDevicePath () string
8080 MappedDeviceName () string
8181 RawDevicePath () string
82- EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (bool , error )
82+ EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (bool , bool , error )
8383 CheckPassphrase (ctx context.Context , luksPassphrase string ) (bool , error )
8484 RotatePassphrase (ctx context.Context , volumeId , previousLUKSPassphrase , luksPassphrase string ) error
8585 IsMappingStale (ctx context.Context ) bool
@@ -121,48 +121,48 @@ func NewDeviceFromMappingPath(
121121}
122122
123123// EnsureDeviceMappedOnHost ensures the specified device is LUKS formatted, opened, and has the current passphrase.
124- func (d * LUKSDevice ) EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , error ) {
124+ func (d * LUKSDevice ) EnsureDeviceMappedOnHost (ctx context.Context , name string , secrets map [string ]string ) (bool , bool , error ) {
125125 // Try to Open with current luks passphrase
126126 luksPassphraseName , luksPassphrase , previousLUKSPassphraseName , previousLUKSPassphrase := GetLUKSPassphrasesFromSecretMap (secrets )
127127 if luksPassphrase == "" {
128- return false , fmt .Errorf ("LUKS passphrase cannot be empty" )
128+ return false , false , fmt .Errorf ("LUKS passphrase cannot be empty" )
129129 }
130130 if luksPassphraseName == "" {
131- return false , fmt .Errorf ("LUKS passphrase name cannot be empty" )
131+ return false , false , fmt .Errorf ("LUKS passphrase name cannot be empty" )
132132 }
133133
134134 Logc (ctx ).WithFields (LogFields {
135135 "volume" : name ,
136136 "luks-passphrase-name" : luksPassphraseName ,
137137 }).Info ("Opening encrypted volume." )
138- luksFormatted , err := d .EnsureFormattedAndOpen (ctx , luksPassphrase )
138+ luksFormatted , safeToFormat , err := d .EnsureFormattedAndOpen (ctx , luksPassphrase )
139139
140140 // If we fail due to a format issue there is no need to try to open the device.
141141 if err == nil || errors .IsFormatError (err ) {
142- return luksFormatted , err
142+ return luksFormatted , safeToFormat , err
143143 }
144144
145145 // If we failed to open, try previous passphrase
146146 if previousLUKSPassphrase == "" {
147147 // Return original error if there is no previous passphrase to use
148- return luksFormatted , fmt .Errorf ("could not open LUKS device; %v" , err )
148+ return luksFormatted , safeToFormat , fmt .Errorf ("could not open LUKS device; %v" , err )
149149 }
150150 if luksPassphrase == previousLUKSPassphrase {
151- return luksFormatted , fmt .Errorf ("could not open LUKS device, previous passphrase matches current" )
151+ return luksFormatted , safeToFormat , fmt .Errorf ("could not open LUKS device, previous passphrase matches current" )
152152 }
153153 if previousLUKSPassphraseName == "" {
154- return luksFormatted , fmt .Errorf ("could not open LUKS device, no previous passphrase name provided" )
154+ return luksFormatted , safeToFormat , fmt .Errorf ("could not open LUKS device, no previous passphrase name provided" )
155155 }
156156 Logc (ctx ).WithFields (LogFields {
157157 "volume" : name ,
158158 "luks-passphrase-name" : previousLUKSPassphraseName ,
159159 }).Info ("Opening encrypted volume." )
160- luksFormatted , err = d .EnsureFormattedAndOpen (ctx , previousLUKSPassphrase )
160+ luksFormatted , safeToFormat , err = d .EnsureFormattedAndOpen (ctx , previousLUKSPassphrase )
161161 if err != nil {
162- return luksFormatted , fmt .Errorf ("could not open LUKS device; %v" , err )
162+ return luksFormatted , safeToFormat , fmt .Errorf ("could not open LUKS device; %v" , err )
163163 }
164164
165- return luksFormatted , nil
165+ return luksFormatted , safeToFormat , nil
166166}
167167
168168// MappedDevicePath returns the location of the LUKS device when opened.
@@ -181,7 +181,11 @@ func (d *LUKSDevice) RawDevicePath() string {
181181}
182182
183183// EnsureFormattedAndOpen ensures the specified device is LUKS formatted and opened.
184- func (d * LUKSDevice ) EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (formatted bool , err error ) {
184+ // Returns two booleans: the first indicates if the device is crypt formatted,
185+ // the second indicates if the device is safe to file format, meaning the device was empty before being crypt formatted.
186+ func (d * LUKSDevice ) EnsureFormattedAndOpen (ctx context.Context , luksPassphrase string ) (
187+ formatted , safeToFileFormat bool , err error ,
188+ ) {
185189 return d .ensureLUKSDevice (ctx , luksPassphrase )
186190}
187191
@@ -197,36 +201,41 @@ func (d *LUKSDevice) IsMappingStale(ctx context.Context) bool {
197201 return d .isMappingStale (ctx )
198202}
199203
200- func (d * LUKSDevice ) ensureLUKSDevice (ctx context.Context , luksPassphrase string ) (bool , error ) {
204+ // ensureLUKSDevice ensures the device is LUKS formatted and opened.
205+ // Returns two booleans: the first indicates if the device is formatted,
206+ // the second indicates if the device is safe to file format, meaning the device was empty before being crypt formatted.
207+ func (d * LUKSDevice ) ensureLUKSDevice (ctx context.Context , luksPassphrase string ) (bool , bool , error ) {
201208 // First check if LUKS device is already opened. This is OK to check even if the device isn't LUKS formatted.
202209 if isOpen , err := d .IsOpen (ctx ); err != nil {
203210 // If the LUKS device isn't found, it means that we need to check if the device is LUKS formatted.
204211 // If it isn't, then we should format it and attempt to open it.
205212 // If any other error occurs, bail out.
206213 if ! errors .IsNotFoundError (err ) {
207214 Logc (ctx ).WithError (err ).Error ("Could not check if device is an open LUKS device." )
208- return false , err
215+ return false , false , err
209216 }
210217 } else if isOpen {
211218 Logc (ctx ).Debug ("Device is LUKS formatted and open." )
212- return true , nil
219+ return true , false , nil
213220 }
214221
215- if err := d .formatUnformattedDevice (ctx , luksPassphrase ); err != nil {
222+ var safeToFileFormat bool
223+ var err error
224+ if safeToFileFormat , err = d .formatUnformattedDevice (ctx , luksPassphrase ); err != nil {
216225 Logc (ctx ).WithError (err ).Error ("Could not LUKS format device." )
217- return false , fmt .Errorf ("could not LUKS format device; %w" , err )
226+ return false , safeToFileFormat , fmt .Errorf ("could not LUKS format device; %w" , err )
218227 }
219228
220229 // At this point, we should be able to open the device.
221230 if err := d .Open (ctx , luksPassphrase ); err != nil {
222231 // At this point, we couldn't open the LUKS device, but we do know
223232 // the device is LUKS formatted because LUKSFormat didn't fail.
224233 Logc (ctx ).WithError (err ).Error ("Could not open LUKS formatted device." )
225- return true , fmt .Errorf ("could not open LUKS device; %v" , err )
234+ return true , safeToFileFormat , fmt .Errorf ("could not open LUKS device; %v" , err )
226235 }
227236
228237 Logc (ctx ).Debug ("Device is LUKS formatted and open." )
229- return true , nil
238+ return true , safeToFileFormat , nil
230239}
231240
232241func GetLUKSPassphrasesFromSecretMap (secrets map [string ]string ) (string , string , string , string ) {
0 commit comments