You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChecksumPolicy.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.checksum.ChecksumAlgorithm;
  21. import org.apache.archiva.checksum.ChecksummedFile;
  22. import org.apache.archiva.checksum.UpdateStatus;
  23. import org.apache.archiva.repository.storage.StorageAsset;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.stereotype.Service;
  28. import java.io.IOException;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import java.util.Properties;
  35. /**
  36. * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
  37. * successfully and completely (or not).
  38. *
  39. *
  40. */
  41. @Service( "postDownloadPolicy#checksum" )
  42. public class ChecksumPolicy
  43. implements PostDownloadPolicy
  44. {
  45. private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
  46. /**
  47. * The IGNORE policy indicates that if the checksum policy is ignored, and
  48. * the state of, contents of, or validity of the checksum files are not
  49. * checked.
  50. */
  51. public static final String IGNORE = "ignore";
  52. /**
  53. * The FAIL policy indicates that if the checksum does not match the
  54. * downloaded file, then remove the downloaded artifact, and checksum
  55. * files, and fail the transfer to the client side.
  56. */
  57. public static final String FAIL = "fail";
  58. /**
  59. * The FIX policy indicates that if the checksum does not match the
  60. * downloaded file, then fix the checksum file locally, and return
  61. * to the client side the corrected checksum.
  62. */
  63. public static final String FIX = "fix";
  64. private List<ChecksumAlgorithm> algorithms = Arrays.asList( ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
  65. private List<String> options = new ArrayList<>( 3 );
  66. public ChecksumPolicy()
  67. {
  68. options.add( FAIL );
  69. options.add( FIX );
  70. options.add( IGNORE );
  71. }
  72. @Override
  73. public void applyPolicy( String policySetting, Properties request, StorageAsset localFile )
  74. throws PolicyViolationException, PolicyConfigurationException
  75. {
  76. if ( "resource".equals( request.getProperty( "filetype" ) ) )
  77. {
  78. return;
  79. }
  80. if ( !options.contains( policySetting ) )
  81. {
  82. // Not a valid code.
  83. throw new PolicyConfigurationException(
  84. "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
  85. options.iterator(), "," ) + "]" );
  86. }
  87. if ( IGNORE.equals( policySetting ) )
  88. {
  89. // Ignore.
  90. log.debug( "Checksum policy set to IGNORE." );
  91. return;
  92. }
  93. if ( !localFile.exists() )
  94. {
  95. // Local File does not exist.
  96. throw new PolicyViolationException(
  97. "Checksum policy failure, local file " + localFile.getPath() + " does not exist to check." );
  98. }
  99. if ( FAIL.equals( policySetting ) && localFile.isFileBased() )
  100. {
  101. ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
  102. if ( checksum.isValidChecksums( algorithms ) )
  103. {
  104. return;
  105. }
  106. for ( ChecksumAlgorithm algorithm : algorithms )
  107. {
  108. Path file = checksum.getChecksumFile( algorithm );
  109. try
  110. {
  111. Files.deleteIfExists( file );
  112. }
  113. catch ( IOException e )
  114. {
  115. log.error("Could not delete file {}", file);
  116. }
  117. }
  118. try
  119. {
  120. localFile.getStorage().removeAsset( localFile );
  121. }
  122. catch ( IOException e )
  123. {
  124. log.error("Could not delete file {}", localFile);
  125. }
  126. throw new PolicyViolationException(
  127. "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
  128. + localFile.getPath() + "." );
  129. }
  130. if ( FIX.equals( policySetting ) && localFile.isFileBased())
  131. {
  132. ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
  133. if ( checksum.fixChecksums( algorithms ).getTotalStatus() != UpdateStatus.ERROR )
  134. {
  135. log.debug( "Checksum policy set to FIX, checksum files have been updated." );
  136. return;
  137. }
  138. else
  139. {
  140. throw new PolicyViolationException(
  141. "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
  142. + localFile.getPath() + "." );
  143. }
  144. }
  145. throw new PolicyConfigurationException(
  146. "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
  147. }
  148. @Override
  149. public String getDefaultOption()
  150. {
  151. return FIX;
  152. }
  153. @Override
  154. public String getId()
  155. {
  156. return "checksum";
  157. }
  158. @Override
  159. public String getName()
  160. {
  161. return "Checksum";
  162. }
  163. @Override
  164. public List<String> getOptions()
  165. {
  166. return options;
  167. }
  168. }